Datetime 如何将字符串转换为指定格式的日期时间?
字符串日期=“8/13/2010”;//年月日Datetime 如何将字符串转换为指定格式的日期时间?,datetime,Datetime,字符串日期=“8/13/2010”;//年月日 System.Globalization.CultureInfo ci =System.Threading.Thread.CurrentThread.CurrentCulture; string CurrDateFormat = ci.DateTimeFormat.ShortDatePattern.ToString() -- **// dd/mm/yyyy** DateTime dt=Convert.ToDateTime(date); ***
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);
这是我写的代码
我想按照当前区域性日期时间格式在UI中显示日期时间
请帮我解决这个问题
谢谢
Kiran G您可以使用:
DateTime result;
DateTime.TryParse("8/13/2010", out result);
对于错误处理,您可以在
DateTime.TryParse
方法周围放置if
语句,因为它返回一个布尔值。请指定您使用的语言或系统,并相应地标记问题