将 mm/dd/yyyy 转换为 yyyymmdd (VB.NET)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6858895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert mm/dd/yyyy to yyyymmdd (VB.NET)
提问by l3_08
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?
有什么方法可以将日期格式转换为:dd/mm/yyyy 到 yyyymmdd 格式?例如从:25/07/2011 到 20110725?在 VB.NET 中?
回答by Jon Skeet
Dates themselves don't haveformats 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
通过使用dd/MM/yyyy
format解析字符串将其解析为 a ,然后使用yyyyMMdd
format将其转换为字符串:
DateTime date = DateTime.ParseExact(text, "dd/MM/yyyy",
CultureInfo.InvariantCulture);
string reformatted = date.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
Or in VB:
或者在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
.)
(并确保您有 . 的导入System.Globalization
。)
However, ideally you should keep it as a DateTime
(or similar) for as long as possible.
但是,理想情况下,您应该DateTime
尽可能长时间地将其作为(或类似的)保存。
回答by Ritz Arlekar
CDate(Datetext).ToString("yyyyMMdd")
回答by Vincent Van Den Berghe
Use the DateTime.ParseExact
method to parse the date, then use DateTimeObj.ToString("yyyyMMdd")
.
使用DateTime.ParseExact
方法解析日期,然后使用DateTimeObj.ToString("yyyyMMdd")
.