c#日期格式化 - TOMMYHU - 专注互联网开发及运营技术,提供相关资料及软件下载,奇趣网络时事评论!
Sep 6

c#日期格式化 不指定

tommyhu , 23:30 , ASP.NET , Comments(0) , Trackbacks(0) , Reads(3014) , Via Original Large | Medium | Small

 

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
日期转化 
DateTime d t= DateTime.Now;'
dt.ToString();'2005-11-5  13:21:25
dt.ToFileTime().ToString();'127756416859912000
dt.ToFileTimeUtc().ToString();127756704859912816
dt.ToLocalTime().ToString();'2005-11-5 21:21:25
dt.ToLongDateString().ToString();'2005-11-5
dt.ToLongTimeString().ToString(); '13:21:25
dt.ToOADate().ToString(); '38661.5565508218
dt.ToShortDateString().ToString(); '2005-11-5
dt.ToShortTimeString().ToString(); '13:21
dt.ToUniversalTime().ToString(); '2005-11-5 5:21:25
dt.Year.ToString(); '2005
dt.Date.ToString(); '2005-11-5 0:00:00
dt.DayOfWeek.ToString(); 'Saturday
dt.DayOfYear.ToString(); '309
dt.Hour.ToString(); '13
dt.Millisecond.ToString(); '441
dt.Minute.ToString(); '30
dt.Month.ToString(); '11
dt.Second.ToString(); '28
dt.Ticks.ToString(); '632667942284412864
dt.TimeOfDay.ToString(); '13:30:28.4412864
dt.ToString(); '2005-11-5 13:47:04
dt.AddYears(1).ToString(); '2006-11-5 13:47:04
dt.AddDays(1.1).ToString(); '2005-11-6 16:11:04
dt.AddHours(1.1).ToString(); '2005-11-5 14:53:04
dt.AddMilliseconds(1.1).ToString(); '2005-11-5 13:47:04
dt.AddMonths(1).ToString(); '2005-12-5 13:47:04
dt.AddSeconds(1.1).ToString(); '2005-11-5 13:47:05
dt.AddMinutes(1.1).ToString(); '2005-11-5 13:48:10
dt.AddTicks(1000).ToString(); '2005-11-5 13:47:04
dt.CompareTo(dt).ToString(); '0
dt.Add(?).ToString(); '问号为一个时间段
dt.Equals("2005-11-6 16:11:04").ToString(); 'False
dt.Equals(dt).ToString(); 'True
dt.GetHashCode().ToString(); '1474088234
dt.GetType().ToString(); 'System.DateTime
dt.GetTypeCode().ToString(); 'DateTime
dt.GetDateTimeFormats('s')[0].ToString(); '2005-11-05T14:06:25
dt.GetDateTimeFormats('t')[0].ToString(); '14:06
dt.GetDateTimeFormats('y')[0].ToString(); '2005年11月
dt.GetDateTimeFormats('D')[0].ToString(); '2005年11月5日
dt.GetDateTimeFormats('D')[1].ToString(); '2005 11 05
dt.GetDateTimeFormats('D')[2].ToString(); '星期六 2005 11 05
dt.GetDateTimeFormats('D')[3].ToString(); '星期六 2005年11月5日
dt.GetDateTimeFormats('M')[0].ToString(); '11月5日
dt.GetDateTimeFormats('f')[0].ToString(); '2005年11月5日 14:06
dt.GetDateTimeFormats('g')[0].ToString(); '2005-11-5 14:06
dt.GetDateTimeFormats('r')[0].ToString(); 'Sat, 05 Nov 2005 14:06:25 GMT
string.Format("{0:d}",dt); '2005-11-5
string.Format("{0:D}",dt); '2005年11月5日
string.Format("{0:f}",dt); '2005年11月5日 14:23
string.Format("{0:F}",dt); '2005年11月5日 14:23:23
string.Format("{0:g}",dt); '2005-11-5 14:23
string.Format("{0:G}",dt); '2005-11-5 14:23:23
string.Format("{0:M}",dt); '11月5日
string.Format("{0:R}",dt); 'Sat, 05 Nov 2005 14:23:23 GMT
string.Format("{0:s}",dt); '2005-11-05T14:23:23
string.Format("{0:t}",dt); '14:23
string.Format("{0:T}",dt); '14:23:23
string.Format("{0:u}",dt); '2005-11-05 14:23:23Z
string.Format("{0:U}",dt); '2005年11月5日 6:23:23
string.Format("{0:Y}",dt); '2005年11月
string.Format("{0}",dt); '2005-11-5 14:23:23
            

string.Format("{0:yyyyMMddHHmmssffff}",dt);

            

比较两时间大小
            
1、比较时间大小的实验
            string st1="12:13";
            string st2="14:14";
            DateTime dt1=Convert.ToDateTime(st1);
            DateTime dt2=Convert.ToDateTime(st2);
            DateTime dt3=DateTime.Now;
            if(DateTime.Compare(dt1,dt2)>0)
            msg.Text=st1+">"+st2;
            else
            msg.Text=st1+"<"+st2;
            msg.Text+="\r\n"+dt1.ToString();
            if(DateTime.Compare(dt1,dt3)>0)
            msg.Text+="\r\n"+st1+">"+dt3.ToString();
            else
            msg.Text+="\r\n"+st1+"<"+dt3.ToString();

            
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()+"分钟"
                         +ts.Seconds.ToString()+"秒";
             }
             catch
             {
            
             }
             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()+"-"+ts.Days.ToString()+"天\r\n";
    msg.Text+=dt2.ToString();
            
'

 


▲返回顶部
Last modified by tommyhu on2012/09/06 23:31

Add a comment

Nickname

emotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemot