Convert mm/dd/yyyy to yyyymmdd (VB.NET)

与世无争的帅哥 提交于 2019-12-22 04:42:33

问题


Is there any way I can convert a date of format: dd/mm/yyyy to yyyymmdd format? For example from : 25/07/2011 to 20110725? in VB.NET?


回答1:


Dates themselves don't have formats inherently. You can parse a string into a DateTime by parsing it with dd/MM/yyyy format and then convert that into a string using yyyyMMdd format:

DateTime date = DateTime.ParseExact(text, "dd/MM/yyyy",
                                    CultureInfo.InvariantCulture);

string reformatted = date.ToString("yyyyMMdd", CultureInfo.InvariantCulture);

Or in VB:

Dim date as DateTime = DateTime.ParseExact(text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Dim reformatted as String = date.ToString("yyyyMMdd", CultureInfo.InvariantCulture)

(And make sure you have an import for System.Globalization.)

However, ideally you should keep it as a DateTime (or similar) for as long as possible.




回答2:


 CDate(Datetext).ToString("yyyyMMdd")



回答3:


Use the DateTime.ParseExact method to parse the date, then use DateTimeObj.ToString("yyyyMMdd").

DaTeTime.ParseExact



来源:https://stackoverflow.com/questions/6858895/convert-mm-dd-yyyy-to-yyyymmdd-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!