But how could you live and have no story to tell!
访问统计 original graphics

最近需要把“20121010”转换为“2012-10-10”格式,直接用Convert.ToDateTime("20121010"),系统报错“未被识别的DateTime类型”。

解决方法:

 

一、DateTime.ParseExact

 

string str = "20121010"


 IFormatProvider ifp = new CultureInfo("zh-CN", true); 

DateTime dt = DateTime.ParseExact(str, "yyyyMMdd", ifp);

MessageBox.Show(dt.ToString("yyyy-MM-dd"));

二、DateTime.TryParseExact

 

string str = "20121010";

 

DateTime dt; 


IFormatProvider ifp = new CultureInfo("zh-CN", true);


if (DateTime.TryParseExact(str, "yyyyMMdd", ifp, DateTimeStyles.None, out dt)) 

{
      MessageBox.Show(dt.ToString(("yyyy-MM-dd"));

}

posted on 2012-10-25 17:29  nextsoft  阅读(22517)  评论(0编辑  收藏  举报