C#日期格式转换大全 下载本文

内容发布更新时间 : 2024/5/21 16:51:13星期一 下面是文章的全部内容请认真阅读。

Label8.Text = dt.GetDateTimeFormats('M')[0].ToString();//11月5日

Label9.Text = dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06 Label10.Text = dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06 Label11.Text = dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT

Label1.Text = string.Format(\

Label2.Text = string.Format(\年11月5日

Label3.Text = string.Format(\年11月5日 14:23 Label4.Text = string.Format(\年11月5日 14:23:23 Label5.Text = string.Format(\Label6.Text = string.Format(\Label7.Text = string.Format(\月5日

Label8.Text = string.Format(\Label9.Text = string.Format(\Label10.Text string.Format(\Label11.Text = string.Format(\

Label12.Text = string.Format(\Label13.Text = string.Format(\年11月5日 6:23:23 Label14.Text = string.Format(\年11月

Label15.Text = string.Format(\Label16.Text = string.Format(\C#比较两时间大小

1、比较时间大小的实验 string st1=\dt1=Convert.ToDateTime(st1); DateTime dt2=Convert.ToDateTime(st2); DateTime dt3=DateTime.Now; if(DateTime.Compare(dt1,dt2)>0) msg.Text=st1+\ else msg.Text=st1+\msg.Text+=\ if(DateTime.Compare(dt1,dt3)>0) msg.Text+=\ else msg.Text+=\

2、计算两个时间差值的函数,返回时间差的绝对值: private string DateDiff(DateTime DateTime1,DateTime DateTime2) { string dateDiff=null; try { TimeSpan ts1=new TimeSpan(DateTime1.Ticks); TimeSpan ts2=new TimeSpan(DateTime2.Ticks); TimeSpan ts=ts1.Subtract(ts2).Duration(); dateDiff=ts.Days.ToString()+\天

\ +ts.Hours.ToString()+\小时\+ts.Minutes.ToString()+\分钟\秒\

} return dateDiff; } 3、实现计算DateTime1-36天=DateTime2的功能

TimeSpan ts=new TimeSpan(40,0,0,0); DateTime dt2=DateTime.Now.Subtract(ts); msg.Text=DateTime.Now.ToString()+\天\\r\\n\msg.Text+=dt2.ToString();