将字符串转换成日期<漆虱忧甘/p>
DateTimedatetime=DateTime.ParseExact("ThuMar119:23:56UTC+0800201桃轾庾殇4",format,cultureInfo);
C#把字符串类型日期转换为日期类型有三种方法
Convert.ToDateTime(string)
string格式有要求,必须是yyyy-MM-ddhh:mm:ss
Convert.ToDateTime(string,IFormatProvider)
DateTimedt;
DateTimeFormatInfodtFormat=newSystem.GlobalizationDateTimeFormatInfo();
dtFormat.ShortDatePattern="yyyy/MM/dd";
dt=Convert.ToDateTime("2011/05/26",dtFormat);
DateTime.ParseExact()
stringdateString="20110526";
DateTimedt=DateTime.ParseExact(dateString,"yyyyMMdd",
System.Globalization.CultureInfo.CurrentCulture);
或者
DateTimedt=DateTime.ParseExact(dateString,"yyyyMMdd",
System.Globalization.CultureInfo.InvariantCulture);
扩展资料:
代码实现:
privatevoidForm1_Load(objectsender,EventArgse)
{
this.timer.Start();//窗体启动时,启动timer。
label_currentTime.Text=DateTime.Now.ToString("当前时间:"+
"yyyy年MM月dd日ddddttHH:mm:ss");
}
/*timer中执行的代码*/
privatevoidtimer_Tick(objectsender,EventArgse)
{
DateTimetime=DateTime.Now;
stringt=time.ToString("当前时间:"+"yyyy年MM月dd日ddddttHH:mm:ss");
this.label_currentTime.Text=t;
}