ASP를 배워라.Net System.DateTime 구조

18572 단어
구성원:
/*    */
DateTime.MaxValue; //9999/12/31 23:59:59
DateTime.MinValue; //0001/01/01 00:00:00

/*      */
DateTime.Now;    //       
DateTime.Today;  //    
DateTime.UtcNow; //       (UTC)

/*      */
DateTime.Compare();         //    ,     -1、0、1
DateTime.DaysInMonth();     //        
DateTime.Equals();          //
DateTime.FromBinary();      //  ToBinary()       
DateTime.FromFileTime();    //  ToFileTime()       
DateTime.FromFileTimeUtc(); //  ToFileTimeUtc()       
DateTime.FromOADate();      //  ToOADate()       
DateTime.IsLeapYear();      //     
DateTime.Parse();           //        
DateTime.ParseExact();      //                 
DateTime.ReferenceEquals(); //
DateTime.SpecifyKind();     //            
DateTime.TryParse();        //          
DateTime.TryParseExact();   //                   

/*    */
Date;        //  
Day;         // (1..31)
DayOfWeek;   // 
DayOfYear;   // (1..366)
Hour;        // 
Kind;        //       
Millisecond; //  
Minute;      // 
Month;       // 
Second;      // 
Ticks;       //     
TimeOfDay;   //  
Year;        // 

/*    */
Add();                  //+ TimeSpan        
AddDays();              //+ 
AddHours();             //+ 
AddMilliseconds();      //+  
AddMinutes();           //+ 
AddMonths();            //+ 
AddSeconds();           //+ 
AddTicks();             //+   
AddYears();             //+ 
CompareTo();            //    ,     -1、0、1
Equals();               //
GetDateTimeFormats();   //           
IsDaylightSavingTime(); //       
Subtract();             //- TimeSpan        
ToBinary();             //     long,     Ticks    
ToFileTime();           //   Windows     , long   
ToFileTimeUtc();        //   UTC     Windows     
ToLocalTime();          //       
ToLongDateString();     //      
ToLongTimeString();     //      
ToOADate();             //   OLE      , double   
ToShortDateString();    //      
ToShortTimeString();    //      
ToString();             //     
ToUniversalTime();      //    UTC   

일반적인 구조 함수:
protected void Button1_Click(object sender, EventArgs e)
{
    const string fmt = "yyyy-MM-dd HH:mm:ss fff
"; string s1 = new DateTime().ToString(fmt); //0001-01-01 00:00:00 000 string s2 = new DateTime(634295342967890000).ToString(fmt); //2011-01-02 03:04:56 789 string s3 = new DateTime(2011, 1, 2).ToString(fmt); //2011-01-02 00:00:00 000 string s4 = new DateTime(2011, 1, 2, 3, 4, 56).ToString(fmt); //2011-01-02 03:04:56 000 string s5 = new DateTime(2011, 1, 2, 3, 4, 56, 789).ToString(fmt); //2011-01-02 03:04:56 789 TextBox1.Text = string.Concat(s1, s2, s3, s4, s5); }

속성 테스트:
protected void Button1_Click(object sender, EventArgs e)
{
    DateTime dt = new DateTime(2011, 1, 2, 3, 4, 56, 789);
    TextBox1.Text = string.Format("{0}
{1}
{2}
{3}
{4}
{5}
{6}
{7}
{8}
{9}
{10}
{11}
{12}", dt.Date, //2011/1/2 0:00:00 dt.Day, //2 dt.DayOfWeek, //Sunday dt.DayOfYear, //2 dt.Hour, //3 dt.Kind, //Unspecified dt.Millisecond, //789 dt.Minute, //4 dt.Month, //1 dt.Second, //56 dt.Ticks, //634295342967890000 dt.TimeOfDay, //03:04:56.7890000 dt.Year //2011 ); }

AddDays():
protected void Button1_Click(object sender, EventArgs e)
{
    DateTime dt = new DateTime(2011, 1, 2); //2011/1/2
    DateTime dt1 = dt.AddDays(1);           //2011/1/3
    DateTime dt2 = dt.AddDays(-2);          //2010/12/31

    TextBox1.Text = string.Format("{0:d}
{1:d}
{2:d}", dt, dt1, dt2); }

DateTime.Parse():
protected void Button1_Click(object sender, EventArgs e)
{
    DateTime dt1 = DateTime.Parse("2011/1/2");               //2011/1/2 0:00:00
    DateTime dt2 = DateTime.Parse("2011 1 2  3 4 56 "); //2011/1/2 3:04:56

    TextBox1.Text = string.Concat(dt1, "
", dt2); }

변환 및 재구성:
protected void Button1_Click(object sender, EventArgs e)
{
    DateTime dt = new DateTime(2011, 1, 2, 3, 4, 56, 789); //2011/1/2 3:04:56
    long binaryTime = dt.ToBinary();       //634295342967890000                
    long fileTime = dt.ToFileTime();       //129383822967890000               
    long fileUtcTime = dt.ToFileTimeUtc(); //129384110967890000               
    double oleTime = dt.ToOADate();        //40545.1284350579               

    DateTime dt1 = DateTime.FromBinary(binaryTime);       //2011/1/2 3:04:56
    DateTime dt2 = DateTime.FromFileTime(fileTime);       //2011/1/2 3:04:56
    DateTime dt3 = DateTime.FromFileTimeUtc(fileUtcTime); //2011/1/2 3:04:56
    DateTime dt4 = DateTime.FromOADate(oleTime);          //2011/1/2 3:04:56

    TextBox1.Text = string.Format("{0}
{1}
{2}
{3}
{4}
{5}
{6}
{7}
{8}", dt, binaryTime, fileTime, fileUtcTime, oleTime, dt1, dt2, dt2, dt4 ); }

DateTime.DaysInMonth():
protected void Button1_Click(object sender, EventArgs e)
{
    int nDay;
    string str = "";
    for (int i=1; i<=12; i++)
    {
        nDay = DateTime.DaysInMonth(2011, i);
        str += string.Format("2011   {0,2}  : {1}  
", i, nDay); } TextBox1.Text = str; } /********************* 2011 1 : 31 2011 2 : 28 2011 3 : 31 2011 4 : 30 2011 5 : 31 2011 6 : 30 2011 7 : 31 2011 8 : 31 2011 9 : 30 2011 10 : 31 2011 11 : 30 2011 12 : 31 **********************/

ToString():
protected void Button1_Click(object sender, EventArgs e)
{
    DateTime dt = new DateTime(2011, 1, 2, 3, 4, 56, 789);
    string[] arr = new string[60];
    arr[0] = dt.ToString();           //2011/1/2 3:04:56
    arr[1] = dt.ToString("d");        //2011/1/2
    arr[2] = dt.ToString("D");        //2011 1 2 
    arr[3] = dt.ToString("f");        //2011 1 2  3:04
    arr[4] = dt.ToString("F");        //2011 1 2  3:04:56
    arr[5] = dt.ToString("g");        //2011/1/2 3:04
    arr[6] = dt.ToString("G");        //2011/1/2 3:04:56
    arr[7] = dt.ToString("m");        //1 2 
    arr[8] = dt.ToString("o");        //2011-01-02T03:04:56.7890000
    arr[9] = dt.ToString("r");        //Sun, 02 Jan 2011 03:04:56 GMT
    arr[10] = dt.ToString("s");       //2011-01-02T03:04:56
    arr[11] = dt.ToString("t");       //3:04
    arr[12] = dt.ToString("T");       //3:04:56
    arr[13] = dt.ToString("u");       //2011-01-02 03:04:56Z
    arr[14] = dt.ToString("U");       //2011 1 1  19:04:56
    arr[15] = dt.ToString("y");       //2011 1 
    arr[16] = dt.ToString("d");       //2011/1/2           
    arr[17] = dt.ToString("dd");      //02           
    arr[18] = dt.ToString("ddd");     //             
    arr[19] = dt.ToString("dddd");    //              
    arr[20] = dt.ToString("f");       //2011 1 2  3:04           
    arr[21] = dt.ToString("ff");      //78           
    arr[22] = dt.ToString("fff");     //789           
    arr[23] = dt.ToString("ffff");    //7890           
    arr[24] = dt.ToString("fffff");   //78900           
    arr[25] = dt.ToString("ffffff");  //789000           
    arr[26] = dt.ToString("fffffff"); //7890000          
    arr[27] = dt.ToString("F");       //2011 1 2  3:04:56          
    arr[28] = dt.ToString("FF");      //78          
    arr[29] = dt.ToString("FFF");     //789          
    arr[30] = dt.ToString("FFFF");    //789          
    arr[31] = dt.ToString("FFFFF");   //789          
    arr[32] = dt.ToString("FFFFFF");  //789          
    arr[33] = dt.ToString("FFFFFFF"); //789          
    arr[34] = dt.ToString("g");       //2011/1/2 3:04          
    arr[35] = dt.ToString("%h");      //3          
    arr[36] = dt.ToString("hh");      //03          
    arr[37] = dt.ToString("%H");      //3          
    arr[38] = dt.ToString("HH");      //03          
    arr[39] = dt.ToString("%K");
    arr[40] = dt.ToString("m");       //1 2           
    arr[41] = dt.ToString("mm");      //04          
    arr[42] = dt.ToString("M");       //1 2           
    arr[43] = dt.ToString("MM");      //01          
    arr[44] = dt.ToString("MMM");     //            
    arr[45] = dt.ToString("MMMM");    //            
    arr[46] = dt.ToString("s");       //2011-01-02T03:04:56          
    arr[47] = dt.ToString("ss");      //56          
    arr[48] = dt.ToString("t");       //3:04          
    arr[49] = dt.ToString("tt");      //            
    arr[50] = dt.ToString("y");       //2011 1           
    arr[51] = dt.ToString("yy");      //11          
    arr[52] = dt.ToString("yyy");     //2011          
    arr[53] = dt.ToString("yyyy");    //2011          
    arr[54] = dt.ToString("yyyyy");   //02011          
    arr[55] = dt.ToString("%z");      //+8          
    arr[56] = dt.ToString("zz");      //+08          
    arr[57] = dt.ToString("zzz");     //+08:00          
    arr[58] = dt.ToString("yyyy MM dd  HH mm ss "); //2011 01 02  03 04 56 
    arr[59] = dt.ToString("yyyy/M/d H:m:s");             //2011/1/2 3:4:56

    string str = "";
    foreach (string s in arr) { str += s + "
"; } TextBox1.Text = str; }

GetDateTimeFormats():
protected void Button1_Click(object sender, EventArgs e)
{
    DateTime dt = new DateTime(2011, 1, 2, 3, 4, 56, 789);
    string[] sArr1 = dt.GetDateTimeFormats();
    string[] sArr2 = dt.GetDateTimeFormats(new System.Globalization.CultureInfo("en-US"));
    TextBox1.Text = string.Join("
", sArr1); TextBox1.Text += "
" + new string('=', 32) + "
"; TextBox1.Text += string.Join("
", sArr2); } /* : 2011/1/2 2011-1-2 2011.1.2 2011-01-02 2011/01/02 2011.01.02 11-1-2 11/1/2 11.1.2 11/01/02 2011 1 2 2011 1 2 , ,2011 1 2 2011 1 2 3:04 2011 1 2 03:04 2011 1 2 3:04 2011 1 2 03:04 2011 1 2 , 3:04 2011 1 2 , 03:04 2011 1 2 , 3:04 2011 1 2 , 03:04 ,2011 1 2 3:04 ,2011 1 2 03:04 ,2011 1 2 3:04 ,2011 1 2 03:04 2011 1 2 3:04:56 2011 1 2 03:04:56 2011 1 2 3:04:56 2011 1 2 03:04:56 2011 1 2 , 3:04:56 2011 1 2 , 03:04:56 2011 1 2 , 3:04:56 2011 1 2 , 03:04:56 ,2011 1 2 3:04:56 ,2011 1 2 03:04:56 ,2011 1 2 3:04:56 ,2011 1 2 03:04:56 2011/1/2 3:04 2011/1/2 03:04 2011/1/2 3:04 2011/1/2 03:04 2011-1-2 3:04 2011-1-2 03:04 2011-1-2 3:04 2011-1-2 03:04 2011.1.2 3:04 2011.1.2 03:04 2011.1.2 3:04 2011.1.2 03:04 2011-01-02 3:04 2011-01-02 03:04 2011-01-02 3:04 2011-01-02 03:04 2011/01/02 3:04 2011/01/02 03:04 2011/01/02 3:04 2011/01/02 03:04 2011.01.02 3:04 2011.01.02 03:04 2011.01.02 3:04 2011.01.02 03:04 11-1-2 3:04 11-1-2 03:04 11-1-2 3:04 11-1-2 03:04 11/1/2 3:04 11/1/2 03:04 11/1/2 3:04 11/1/2 03:04 11.1.2 3:04 11.1.2 03:04 11.1.2 3:04 11.1.2 03:04 11/01/02 3:04 11/01/02 03:04 11/01/02 3:04 11/01/02 03:04 2011/1/2 3:04:56 2011/1/2 03:04:56 2011/1/2 3:04:56 2011/1/2 03:04:56 2011-1-2 3:04:56 2011-1-2 03:04:56 2011-1-2 3:04:56 2011-1-2 03:04:56 2011.1.2 3:04:56 2011.1.2 03:04:56 2011.1.2 3:04:56 2011.1.2 03:04:56 2011-01-02 3:04:56 2011-01-02 03:04:56 2011-01-02 3:04:56 2011-01-02 03:04:56 2011/01/02 3:04:56 2011/01/02 03:04:56 2011/01/02 3:04:56 2011/01/02 03:04:56 2011.01.02 3:04:56 2011.01.02 03:04:56 2011.01.02 3:04:56 2011.01.02 03:04:56 11-1-2 3:04:56 11-1-2 03:04:56 11-1-2 3:04:56 11-1-2 03:04:56 11/1/2 3:04:56 11/1/2 03:04:56 11/1/2 3:04:56 11/1/2 03:04:56 11.1.2 3:04:56 11.1.2 03:04:56 11.1.2 3:04:56 11.1.2 03:04:56 11/01/02 3:04:56 11/01/02 03:04:56 11/01/02 3:04:56 11/01/02 03:04:56 1 2 1 2 2011-01-02T03:04:56.7890000 2011-01-02T03:04:56.7890000 Sun, 02 Jan 2011 03:04:56 GMT Sun, 02 Jan 2011 03:04:56 GMT 2011-01-02T03:04:56 3:04 03:04 3:04 03:04 3:04:56 03:04:56 3:04:56 03:04:56 2011-01-02 03:04:56Z 2011 1 1 19:04:56 2011 1 1 19:04:56 2011 1 1 7:04:56 2011 1 1 07:04:56 2011 1 1 , 19:04:56 2011 1 1 , 19:04:56 2011 1 1 , 7:04:56 2011 1 1 , 07:04:56 ,2011 1 1 19:04:56 ,2011 1 1 19:04:56 ,2011 1 1 7:04:56 ,2011 1 1 07:04:56 2011 1 2011.1 2011 1 2011.1 ================================ 1/2/2011 1/2/11 01/02/11 01/02/2011 11/01/02 2011-01-02 02-Jan-11 Sunday, January 02, 2011 January 02, 2011 Sunday, 02 January, 2011 02 January, 2011 Sunday, January 02, 2011 3:04 AM Sunday, January 02, 2011 03:04 AM Sunday, January 02, 2011 3:04 Sunday, January 02, 2011 03:04 January 02, 2011 3:04 AM January 02, 2011 03:04 AM January 02, 2011 3:04 January 02, 2011 03:04 Sunday, 02 January, 2011 3:04 AM Sunday, 02 January, 2011 03:04 AM Sunday, 02 January, 2011 3:04 Sunday, 02 January, 2011 03:04 02 January, 2011 3:04 AM 02 January, 2011 03:04 AM 02 January, 2011 3:04 02 January, 2011 03:04 Sunday, January 02, 2011 3:04:56 AM Sunday, January 02, 2011 03:04:56 AM Sunday, January 02, 2011 3:04:56 Sunday, January 02, 2011 03:04:56 January 02, 2011 3:04:56 AM January 02, 2011 03:04:56 AM January 02, 2011 3:04:56 January 02, 2011 03:04:56 Sunday, 02 January, 2011 3:04:56 AM Sunday, 02 January, 2011 03:04:56 AM Sunday, 02 January, 2011 3:04:56 Sunday, 02 January, 2011 03:04:56 02 January, 2011 3:04:56 AM 02 January, 2011 03:04:56 AM 02 January, 2011 3:04:56 02 January, 2011 03:04:56 1/2/2011 3:04 AM 1/2/2011 03:04 AM 1/2/2011 3:04 1/2/2011 03:04 1/2/11 3:04 AM 1/2/11 03:04 AM 1/2/11 3:04 1/2/11 03:04 01/02/11 3:04 AM 01/02/11 03:04 AM 01/02/11 3:04 01/02/11 03:04 01/02/2011 3:04 AM 01/02/2011 03:04 AM 01/02/2011 3:04 01/02/2011 03:04 11/01/02 3:04 AM 11/01/02 03:04 AM 11/01/02 3:04 11/01/02 03:04 2011-01-02 3:04 AM 2011-01-02 03:04 AM 2011-01-02 3:04 2011-01-02 03:04 02-Jan-11 3:04 AM 02-Jan-11 03:04 AM 02-Jan-11 3:04 02-Jan-11 03:04 1/2/2011 3:04:56 AM 1/2/2011 03:04:56 AM 1/2/2011 3:04:56 1/2/2011 03:04:56 1/2/11 3:04:56 AM 1/2/11 03:04:56 AM 1/2/11 3:04:56 1/2/11 03:04:56 01/02/11 3:04:56 AM 01/02/11 03:04:56 AM 01/02/11 3:04:56 01/02/11 03:04:56 01/02/2011 3:04:56 AM 01/02/2011 03:04:56 AM 01/02/2011 3:04:56 01/02/2011 03:04:56 11/01/02 3:04:56 AM 11/01/02 03:04:56 AM 11/01/02 3:04:56 11/01/02 03:04:56 2011-01-02 3:04:56 AM 2011-01-02 03:04:56 AM 2011-01-02 3:04:56 2011-01-02 03:04:56 02-Jan-11 3:04:56 AM 02-Jan-11 03:04:56 AM 02-Jan-11 3:04:56 02-Jan-11 03:04:56 January 02 January 02 2011-01-02T03:04:56.7890000 2011-01-02T03:04:56.7890000 Sun, 02 Jan 2011 03:04:56 GMT Sun, 02 Jan 2011 03:04:56 GMT 2011-01-02T03:04:56 3:04 AM 03:04 AM 3:04 03:04 3:04:56 AM 03:04:56 AM 3:04:56 03:04:56 2011-01-02 03:04:56Z Saturday, January 01, 2011 7:04:56 PM Saturday, January 01, 2011 07:04:56 PM Saturday, January 01, 2011 19:04:56 Saturday, January 01, 2011 19:04:56 January 01, 2011 7:04:56 PM January 01, 2011 07:04:56 PM January 01, 2011 19:04:56 January 01, 2011 19:04:56 Saturday, 01 January, 2011 7:04:56 PM Saturday, 01 January, 2011 07:04:56 PM Saturday, 01 January, 2011 19:04:56 Saturday, 01 January, 2011 19:04:56 01 January, 2011 7:04:56 PM 01 January, 2011 07:04:56 PM 01 January, 2011 19:04:56 01 January, 2011 19:04:56 January, 2011 January, 2011 */

좋은 웹페이지 즐겨찾기