将 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:37:46  来源:igfitidea点击:

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

vb.netvb.net-2010

提问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 DateTimeby parsing it with dd/MM/yyyyformat and then convert that into a string using yyyyMMddformat:

日期本身不具有格式固有的。您可以DateTime通过使用dd/MM/yyyyformat解析字符串将其解析为 a ,然后使用yyyyMMddformat将其转换为字符串:

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.ParseExactmethod to parse the date, then use DateTimeObj.ToString("yyyyMMdd").

使用DateTime.ParseExact方法解析日期,然后使用DateTimeObj.ToString("yyyyMMdd").

DaTeTime.ParseExact

日期时间.ParseExact