欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中比較常用的DateTime結(jié)構(gòu)的使用方法

 更新時(shí)間:2015年11月05日 10:25:30   投稿:lijiao  
這篇文章主要介紹了C#中比較常用的DateTime結(jié)構(gòu)的使用方法,需要的朋友可以參考下

在項(xiàng)目開發(fā)中,經(jīng)常會(huì)碰到日期處理。比如查詢中,可能會(huì)經(jīng)常遇到按時(shí)間段查詢,有時(shí)會(huì)默認(rèn)取出一個(gè)月的數(shù)據(jù)。當(dāng)我們提交數(shù)據(jù)時(shí),會(huì)需要記錄當(dāng)前日期,等等。下面就看看一些常用的方法

首先,DateTime是一個(gè)struct。很多時(shí)候,會(huì)把它當(dāng)成一個(gè)類。但它真的不是,MSDN上的描述如下:

DateTime結(jié)構(gòu):表示時(shí)間上的一刻,通常以日期和當(dāng)天的時(shí)間表示。語法:

[SerializableAttribute]
public struct DateTime : IComparable, IFormattable, 
  IConvertible, ISerializable, IComparable<DateTime>, IEquatable<DateTime>

一、DateTime.Now屬性

實(shí)例化一個(gè)DateTime對(duì)象,可以將指定的數(shù)字作為年月日得到一個(gè)DateTime對(duì)象。而DateTime.Now屬性則可獲得當(dāng)前時(shí)間。如果你想按年、月、日分別統(tǒng)計(jì)數(shù)據(jù),也可用DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day獲取。同理,當(dāng)前的時(shí)分秒也可以這樣的方式獲取。還可以在當(dāng)前時(shí)間加上一個(gè)段時(shí)間等操作。    

static void Main(string[] args)
    {
      DateTime newChina = new DateTime(1949, 10, 1);
      Console.WriteLine(newChina);
      Console.WriteLine("當(dāng)前時(shí)間:");
      Console.WriteLine("{0}年,{1}月,{2}日",DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
      Console.WriteLine("{0}時(shí),{1}分, {2}秒",DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
      Console.WriteLine("三天后:{0}",DateTime.Now.AddDays(3));
      Console.ReadLine();
    }

結(jié)果:

二、ToString方法

 DateTime的ToString方法有四種重載方式。其中一個(gè)重載方式允許傳入String,這就意味著你可以將當(dāng)前DateTime對(duì)象轉(zhuǎn)換成等效的字符串形式。比如我們將當(dāng)前時(shí)間輸出,日期按yyyy-mm-dd格式,時(shí)間按hh:mm:ss格式。

Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd"));
  Console.WriteLine(DateTime.Now.ToString("hh:mm:ss"));

還有一個(gè)重載形式是需要提供IFormatProvider,使用指定的區(qū)域性特定格式信息將當(dāng)前 DateTime 對(duì)象的值轉(zhuǎn)換為它的等效字符串表示形式。

static void Main(string[] args)
    {
      CultureInfo jaJP = new CultureInfo("ja-JP");
      jaJP.DateTimeFormat.Calendar = new JapaneseCalendar();
      DateTime date1 = new DateTime(1867, 1, 1);
      DateTime date2 = new DateTime(1967, 1, 1);
      
      try
      {
        Console.WriteLine(date2.ToString(jaJP));
        Console.WriteLine(date1.ToString(jaJP));
      }
      catch (ArgumentOutOfRangeException)
      {
        Console.WriteLine("{0:d} is earlier than {1:d} or later than {2:d}",
                 date1,
                 jaJP.DateTimeFormat.Calendar.MinSupportedDateTime,
                 jaJP.DateTimeFormat.Calendar.MaxSupportedDateTime);
      }
      Console.ReadLine();
    }

結(jié)果:

三、DaysInMonth方法及IsLeapYear方法

 DaysInMonth方法需要兩個(gè)Int32型參數(shù),返回指定年份指定月份的天數(shù)。關(guān)于月份的天數(shù),多數(shù)只有2月需要特殊照顧一下。剩余的月份,無論哪一年的天數(shù)都是固定的。而二月呢,不但不是其他月份的30天或31天,她還分個(gè)閏年非閏年。

static void Main(string[] args)
    {
      Console.WriteLine("2000年至2015年中二月的天數(shù)");
      for (int i = 2000; i < 2015; i++)
      {
        Console.WriteLine("{0}年2月有:{1}天", i, DateTime.DaysInMonth(i, 2));
      }
      Console.ReadLine();
    }

輸出結(jié)果:

從輸出結(jié)果中可以看出,2月為29天的年份為閏年。但其實(shí)DateTime還提供了判斷閏年的方法IsLeapYear,該方法只要一個(gè)Int32的參數(shù),若輸入的年份是閏年返回true,否則返回false。(.Net Framework就是這么貼心,你要的東西都給你封裝好了,直接拿來用好了。)要是沒這個(gè)方法呢,得自己去按照閏年的規(guī)則去寫個(gè)小方法來判斷。

static void Main(string[] args)
    {
      Console.WriteLine("2000年至2015年中二月的天數(shù)");
      for (int i = 2000; i < 2015; i++)
      {
        if (DateTime.IsLeapYear(i))
          Console.WriteLine("{0}年是閏年,2月有{1}天", i, DateTime.DaysInMonth(i, 2));
        else
          Console.WriteLine("{0}年是平年,2月有{1}天",i,DateTime.DaysInMonth(i,2));
      }
      Console.ReadLine();
    }

微軟現(xiàn)在已經(jīng)將.NetFramework開源了,這意味著可以自己去查看源代碼了。附上DateTime.cs的源碼鏈接,以及IsLeapYear方法的源代碼。雖然僅僅兩三行代碼,但在實(shí)際開發(fā)中,你可能一時(shí)間想不起閏年的計(jì)算公式,或者拿捏不準(zhǔn)。封裝好的方法為你節(jié)省大量時(shí)間。

DateTime.cs源碼中IsLeapYear方法

   // Checks whether a given year is a leap year. This method returns true if
    // year is a leap year, or false if not.
    //
    public static bool IsLeapYear(int year) {
      if (year < 1 || year > 9999) {
        throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_Year"));
      }
      Contract.EndContractBlock();
      return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    }

文章中介紹了幾個(gè)算是比較常用的方法,希望對(duì)大家的學(xué)習(xí)有所幫助,平時(shí)多閱讀相關(guān)文章,積累經(jīng)驗(yàn)。

相關(guān)文章

最新評(píng)論