如何将字符串转换为24小时格式的日期时间 [英] How do i convert string to datetime of 24 hr format

查看:121
本文介绍了如何将字符串转换为24小时格式的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我试图将字符串转换为24小时格式的日期时间。但以下代码总是返回12小时格式。



c#



I tried to convert a string to datetime of 24 hour format. but the following code always return 12 hours format.

c#

queryStartDate = DateTime.ParseExact(dmqlDate.Substring(0, 19), "yyyy-MM-ddTHH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);





请帮我解决这个问题。



谢谢,

Aravind



我尝试过:



queryStartDate = DateTime.ParseExact(dmqlDate.Substring(0,19),yyyy-MM-ddTHH:mm:ss,System.Globalization.CultureInfo.InvariantCulture);



Please help me to solve this.

Thanks,
Aravind

What I have tried:

queryStartDate = DateTime.ParseExact(dmqlDate.Substring(0, 19), "yyyy-MM-ddTHH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

推荐答案

那是因为DateTime值没有任何格式:它们没有AM / PM或24小时的概念,因为它们自预定义的时间点以来被存储为多个刻度。

他们只有在将它们转换回字符串以便呈现给用户时才会获得格式,然后通常会以他喜欢的格式进行格式化。
That's because DateTime values don't have any formatting: they have no concept of "AM/PM" or "24 hour" becuase they are stored as a number of ticks since a predefined point in time.
They only acquire formatting when they are converted back to a string for presentation to the user, and would then normally be formatted in his prefered format.


日期是一个日期,仅此而已!



当您将字符串转换为DateTime时,您将获得DateTime,而不是24小时格式的字符串!

根据操作系统设置(区域设置),日期的字符串表示可能不一样。

请参阅:

标准日期和时间格式字符串 [ ^ ]

自定义日期和时间格式字符串 [ ^ ]



检查:

Date is a date and nothing more!

When you convert string into DateTime, you'll get DateTime, not a string in 24hrs format!
Depending on OS settings (locales), string representation of date may differ.
See:
Standard Date and Time Format Strings[^]
Custom Date and Time Format Strings[^]

Check this:
var dmqlDate = "2015-04-12T23:59:22;SomeStringAfterDate";
var queryStartDate = DateTime.ParseExact(dmqlDate.Substring(0, 19), "yyyy-MM-ddTHH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

Console.WriteLine(queryStartDate.ToString("f", System.Globalization.CultureInfo.CreateSpecificCulture("de-DE")));
Console.WriteLine(queryStartDate.ToString("F", System.Globalization.CultureInfo.CreateSpecificCulture("en-US")));
Console.WriteLine(queryStartDate.ToString("yyyy-MM-dd h:m:stt", System.Globalization.CultureInfo.CreateSpecificCulture("pl-PL")));



以上代码返回:


Above code returns:

Sonntag, 12. April 2015 23:59
Sunday, April 12, 2015 11:59:22 PM
2015-04-12 11:59:22


这篇关于如何将字符串转换为24小时格式的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆