如何将字符串转换为指定格式的DateTime? [英] How to Convert String to DateTime in specified format?
本文介绍了如何将字符串转换为指定格式的DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string date =8/13/2010; // mm / dd / yyyy
System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
string CurrDateFormat = ci.DateTimeFormat.ShortDatePattern.ToString() - ** // dd / mm / yyyy **
DateTime dt = Convert.ToDateTime(date); *** //异常:有效日期时间格式***
date = dt.ToString(CurrDateFormat,ci);
这是我写的代码。
我想按照当前文化日期时间格式在UI中显示DateTime
请帮助我解决问题。 >
感谢
Kiran G
解决方案
您可以使用: p>
DateTime结果;
DateTime.TryParse(8/13/2010,输出结果);
对于错误处理,如果 DateTime.TryParse
方法,因为这将返回一个 boolean
值。
string date="8/13/2010"; // mm/dd/yyyy
System.Globalization.CultureInfo ci =System.Threading.Thread.CurrentThread.CurrentCulture;
string CurrDateFormat = ci.DateTimeFormat.ShortDatePattern.ToString() -- **// dd/mm/yyyy**
DateTime dt=Convert.ToDateTime(date); ***// Exception : In valid date time format***
date=dt.ToString(CurrDateFormat ,ci);
This is the code i written.
I want to display DateTime in UI as per the current Culture Date Time Format
Please help me how to solve the problem.
Thanks Kiran G
解决方案
You could use:
DateTime result;
DateTime.TryParse("8/13/2010", out result);
For error handling, you can put an if
statement around the DateTime.TryParse
method as this returns a boolean
value.
这篇关于如何将字符串转换为指定格式的DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文