简体   繁体   English

将日期字符串转换为日期时间

[英]convert date string to datetime

I have date string in format dd-MMM-yyyy and want to convert this to datetime , when I use below code 我使用dd-MMM-yyyy格式的日期字符串,并想将其转换为datetime ,当我使用以下代码时

DateTime.ParseExact("20-Oct-2012", "yyyy-MM-dd HH:mm tt", null) 

it causing an error 它导致错误

String was not recognized as a valid DateTime. 无法将字符串识别为有效的DateTime。

When I modify above code 当我修改上面的代码

DateTime.ParseExact("20-Oct-2012", "dd-MMM-yyyy", null) 

then I got date time in format ( mm/dd/yyyy ) : 10/20/2012 12:00:00 AM 然后我得到日期时间的格式( mm/dd/yyyy ): 10/20/2012 12:00:00 AM

But I need it should be converted in yyyy/mm/dd format. 但我需要将其转换为yyyy/mm/dd格式。 Please help me in this regard. 请在这方面帮助我。

You should try this 你应该试试这个

DateTime.ParseExact("20-Oct-2012", "dd-MMM-yyyy", null).ToString("yyyy/mm/dd")

For further reading on formats Check This 有关格式的更多信息, 请检查此

You need to distinguish between two separate concerns: that of parsing your original string into an abstract DateTime representation, and that of converting the latter back into another string representation. 您需要区分两个独立的关注点:将原始字符串解析为抽象的DateTime表示,以及将后者转换回另一个字符串表示。

In your code, you're only tackling the former, and relying on the implicit ToString() method call (which uses the system's current locale) to convert it back to string. 在您的代码中,您只需处理前者,并依靠隐式ToString()方法调用(使用系统的当前语言环境)将其转换回字符串。 If you want to control the output format, you need to specify it explicitly: 如果要控制输出格式,则需要明确指定它:

// Convert from string in "dd-MMM-yyyy" format to DateTime.
DateTime dt = DateTime.ParseExact("20-Oct-2012", "dd-MMM-yyyy", null);

// Convert from DateTime to string in "yyyy/MM/dd" format.
string str = dt.ToString("yyyy/MM/dd");

Also note that the mm format specifier represents minutes; 另请注意, mm格式说明符表示分钟; months are represented by MM . 月份以MM表示。

Edit : 'Converted date contain value "10/20/2012 12:00:00 AM".' 编辑 :“转换后的日期包含值“ 2012年10月20日12:00:00”。” Be careful what you mean by that. 请注意您的意思。 The constructed DateTime value contains an abstract representation of the parsed date and time that is independent of any format. 构造的DateTime值包含解析后的日期和时间的抽象表示形式,该表示形式独立于任何格式。

However, in order to display it, you need to convert it back into some string representation. 但是,为了显示它,您需要将其转换回一些字符串表示形式。 When you view the variable in the debugger (as you're presumably doing), Visual Studio automatically calls the parameterless ToString() method on the DateTime , which renders the date and time under the current culture (which, in your case, assumes the US culture). 当您在调试器中查看变量时(大概正在做),Visual Studio会自动在DateTime上调用无参数的ToString()方法,该方法将在当前区域性下呈现日期和时间(在您的情况下,假定日期和时间为美国文化)。

To alter this behaviour such that it renders the date and time under a custom format, you need to explicitly call the ToString(string) overload (or one of the other overloads), as I've shown in the example above. 如上例所示,要更改此行为,使其以自定义格式呈现日期和时间,您需要显式调用ToString(string)重载(或其他重载之一)。

You could try this instead : 您可以尝试以下方法:

Convert.ToDateTime("20-Oct-2012").ToString("yyyy/MM/dd")

Hope this will help !! 希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM