DateTime 小记

 

[1] ToString

 

//  This code example demonstrates the ToString(String) and 
//  ToString(String, IFormatProvider) methods for the DateTime 
//  type in conjunction with the standard date and time 
//  format specifiers.

using  System;
using  System.Globalization;
using  System.Threading;

class  Sample 
{
    
public   static   void  Main() 
    {
    
string  msgShortDate  =   " (d) Short date: . . . . . . .  " ;
    
string  msgLongDate   =   " (D) Long date:. . . . . . . .  " ;
    
string  msgShortTime  =   " (t) Short time: . . . . . . .  " ;
    
string  msgLongTime   =   " (T) Long time:. . . . . . . .  " ;
    
string  msgFullDateShortTime  =  
                          
" (f) Full date/short time: . .  " ;
    
string  msgFullDateLongTime  =
                          
" (F) Full date/long time:. . .  " ;
    
string  msgGeneralDateShortTime  =  
                          
" (g) General date/short time:.  " ;
    
string  msgGeneralDateLongTime  =  
                          
" (G) General date/long time (default): "   +
                          
"     . . . . . . . . . . . . .  " ;
    
string  msgMonth    =     " (M) Month:. . . . . . . . . .  " ;
    
string  msgRFC1123  =     " (R) RFC1123:. . . . . . . . .  " ;
    
string  msgSortable  =    " (s) Sortable: . . . . . . . .  " ;
    
string  msgUniSortInvariant  =  
                          
" (u) Universal sortable (invariant): "   +  
                          
"     . . . . . . . . . . . . .  " ;
    
string  msgUniSort  =     " (U) Universal sortable: . . .  " ;
    
string  msgYear  =        " (Y) Year: . . . . . . . . . .  " ;
    
string  msgRoundtripLocal         =   " (o) Roundtrip (local):. . . .  " ;
    
string  msgRoundtripUTC           =   " (o) Roundtrip (UTC):. . . . .  " ;
    
string  msgRoundtripUnspecified   =   " (o) Roundtrip (Unspecified):.  " ;


    
string  msg1  =   " Use ToString(String) and the current thread culture. " ;
    
string  msg2  =   " Use ToString(String, IFormatProvider) and a specified culture. " ;
    
string  msgCulture   =   " Culture: " ;
    
string  msgThisDate  =   " This date and time: {0} " ;

    DateTime thisDate  
=  DateTime.Now;
    DateTime  utcDate  
=  thisDate.ToUniversalTime();
    DateTime unspecifiedDate 
=   new  DateTime( 2000 3 20 13 2 3 0 , DateTimeKind.Unspecified);
    CultureInfo ci;

//  Format the current date and time in various ways.
    Console.Clear();
    Console.WriteLine(
" Standard DateTime Format Specifiers: " );
    Console.WriteLine(msgThisDate, thisDate);
    Console.WriteLine(msg1);

//  Display the thread current culture, which is used to format the values.
    ci  =  Thread.CurrentThread.CurrentCulture;
    Console.WriteLine(
" {0,-30}{1} " , msgCulture, ci.DisplayName);

    Console.WriteLine(msgShortDate            
+          thisDate.ToString( " d " ));
    Console.WriteLine(msgLongDate             
+          thisDate.ToString( " D " ));
    Console.WriteLine(msgShortTime            
+          thisDate.ToString( " t " ));
    Console.WriteLine(msgLongTime             
+          thisDate.ToString( " T " ));
    Console.WriteLine(msgFullDateShortTime    
+          thisDate.ToString( " f " ));
    Console.WriteLine(msgFullDateLongTime     
+          thisDate.ToString( " F " ));
    Console.WriteLine(msgGeneralDateShortTime 
+          thisDate.ToString( " g " ));
    Console.WriteLine(msgGeneralDateLongTime  
+          thisDate.ToString( " G " ));
    Console.WriteLine(msgMonth                
+          thisDate.ToString( " M " ));
    Console.WriteLine(msgRFC1123              
+           utcDate.ToString( " R " ));
    Console.WriteLine(msgSortable             
+          thisDate.ToString( " s " ));
    Console.WriteLine(msgUniSortInvariant     
+           utcDate.ToString( " u " ));
    Console.WriteLine(msgUniSort              
+          thisDate.ToString( " U " ));
    Console.WriteLine(msgYear                 
+          thisDate.ToString( " Y " ));
    Console.WriteLine(msgRoundtripLocal       
+          thisDate.ToString( " o " ));
    Console.WriteLine(msgRoundtripUTC         
+           utcDate.ToString( " o " ));
    Console.WriteLine(msgRoundtripUnspecified 
+   unspecifiedDate.ToString( " o " ));

    Console.WriteLine();

//  Display the same values using a CultureInfo object. The CultureInfo class 
//  implements IFormatProvider.
    Console.WriteLine(msg2);

//  Display the culture used to format the values. 
    ci  =   new  CultureInfo( " de-DE " );
    Console.WriteLine(
" {0,-30}{1} " , msgCulture, ci.DisplayName);

    Console.WriteLine(msgShortDate            
+          thisDate.ToString( " d " , ci));
    Console.WriteLine(msgLongDate             
+          thisDate.ToString( " D " , ci));
    Console.WriteLine(msgShortTime            
+          thisDate.ToString( " t " , ci));
    Console.WriteLine(msgLongTime             
+          thisDate.ToString( " T " , ci));
    Console.WriteLine(msgFullDateShortTime    
+          thisDate.ToString( " f " , ci));
    Console.WriteLine(msgFullDateLongTime     
+          thisDate.ToString( " F " , ci));
    Console.WriteLine(msgGeneralDateShortTime 
+          thisDate.ToString( " g " , ci));
    Console.WriteLine(msgGeneralDateLongTime  
+          thisDate.ToString( " G " , ci));
    Console.WriteLine(msgMonth                
+          thisDate.ToString( " M " , ci));
    Console.WriteLine(msgRFC1123              
+          utcDate.ToString( " R " , ci));
    Console.WriteLine(msgSortable             
+          thisDate.ToString( " s " , ci));
    Console.WriteLine(msgUniSortInvariant     
+          utcDate.ToString( " u " , ci));
    Console.WriteLine(msgUniSort              
+          thisDate.ToString( " U " , ci));
    Console.WriteLine(msgYear                 
+          thisDate.ToString( " Y " , ci));
    Console.WriteLine(msgRoundtripLocal       
+          thisDate.ToString( " o " , ci));
    Console.WriteLine(msgRoundtripUTC         
+           utcDate.ToString( " o " , ci));
    Console.WriteLine(msgRoundtripUnspecified 
+   unspecifiedDate.ToString( " o " , ci));

    Console.WriteLine();
    }
}
/*
This code example produces the following results:

Standard DateTime Format Specifiers:

This date and time: 4/17/2006 2:22:48 PM

Use ToString(String) and the current thread culture.

Culture:                      English (United States)

(d) Short date: . . . . . . . 4/17/2006
(D) Long date:. . . . . . . . Monday, April 17, 2006
(t) Short time: . . . . . . . 2:22 PM
(T) Long time:. . . . . . . . 2:22:48 PM
(f) Full date/short time: . . Monday, April 17, 2006 2:22 PM
(F) Full date/long time:. . . Monday, April 17, 2006 2:22:48 PM
(g) General date/short time:. 4/17/2006 2:22 PM
(G) General date/long time (default):
    . . . . . . . . . . . . . 4/17/2006 2:22:48 PM
(M) Month:. . . . . . . . . . April 17
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:22:48 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:22:48
(u) Universal sortable (invariant):
    . . . . . . . . . . . . . 2006-04-17 21:22:48Z
(U) Universal sortable: . . . Monday, April 17, 2006 9:22:48 PM
(Y) Year: . . . . . . . . . . April, 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:22:48.2698750-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:22:48.2698750Z
(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000

Use ToString(String, IFormatProvider) and a specified culture.

Culture:                      German (Germany)

(d) Short date: . . . . . . . 17.04.2006
(D) Long date:. . . . . . . . Montag, 17. April 2006
(t) Short time: . . . . . . . 14:22
(T) Long time:. . . . . . . . 14:22:48
(f) Full date/short time: . . Montag, 17. April 2006 14:22
(F) Full date/long time:. . . Montag, 17. April 2006 14:22:48
(g) General date/short time:. 17.04.2006 14:22
(G) General date/long time (default):
    . . . . . . . . . . . . . 17.04.2006 14:22:48
(M) Month:. . . . . . . . . . 17 April
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:22:48 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:22:48
(u) Universal sortable (invariant):
    . . . . . . . . . . . . . 2006-04-17 21:22:48Z
(U) Universal sortable: . . . Montag, 17. April 2006 21:22:48
(Y) Year: . . . . . . . . . . April 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:22:48.2698750-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:22:48.2698750Z
(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000
*/

 

[2] [日期随机函数]

 

// 日期随机函数#region 日期随机函数
         /**//* *********************************
         * 函数名称:DateRndName
         * 功能说明:日期随机函数
         * 参    数:ra:随机数
         * 调用示例:
         *          GetRemoteObj o = new GetRemoteObj();
         *          Random ra = new Random();
         *          string s = o.DateRndName(ra);
         *          Response.Write(s);
         *          o.Dispose();
         * *******************************
*/

        
public    string  DateRndName(Random ra)
        {
            DateTime d 
=  DateTime.Now;
            
string  s  =   null , y, m, dd, h, mm, ss;
            y 
=  d.Year.ToString();
            m 
=  d.Month.ToString();
            
if  (m.Length  <   2 ) m  =   " 0 "   +  m;
            dd 
=  d.Day.ToString();
            
if  (dd.Length  <   2 ) dd  =   " 0 "   +  dd;
            h 
=  d.Hour.ToString();
            
if  (h.Length  <   2 ) h  =   " 0 "   +  h;
            mm 
=  d.Minute.ToString();
            
if  (mm.Length  <   2 ) mm  =   " 0 "   +  mm;
            ss 
=  d.Second.ToString();
            
if  (ss.Length  <   2 ) ss  =   " 0 "   +  ss;
            s 
+=  y  +   ' , '   +  m  +   ' , '   +  dd  +   ' , '   +  h  +   " - "   +  mm  +   " - "   +  ss;
            s 
+=  ra.Next( 1000000 9999999 ).ToString();
            
return  s;
        }

 

[3] 计算两个时间差值的函数

 

         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;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值