本文介绍了字符串的日期时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将字符串转换为DateTime格式?例如,如果我有一个字符串,如:
24/10/2008
如何获取到DateTime格式?
解决方案
使用:
string str =24/10/2008;
DateTime dt = DateTime.ParseExact(str,dd / MM / yyyy,
Thread.CurrentThread.CurrentCulture);
(你应该考虑你真正想要解析的文化,诚然。)
编辑:其他答案已经指定null作为第三个参数 - 这相当于使用 Thread.CurrentThread.CurrentCulture
。 / p>
对于其他格式,请参阅。
How do I convert a string to DateTime format? For example, if I had a string like:
"24/10/2008"
How do I get that into DateTime format ?
解决方案
Use DateTime.ParseExact:
string str = "24/10/2008";
DateTime dt = DateTime.ParseExact(str, "dd/MM/yyyy",
Thread.CurrentThread.CurrentCulture);
(You should consider what culture you actually want to parse it in, admittedly.)
EDIT: Other answers have specified "null" as the third parameter - this is equivalent to using Thread.CurrentThread.CurrentCulture
.
For other formats, see "Custom Date and Time Format Strings" in MSDN.
这篇关于字符串的日期时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!