好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#中比较常用的DateTime结构的使用方法

在项目开发中,经常会碰到日期处理。比如查询中,可能会经常遇到按时间段查询,有时会默认取出一个月的数据。当我们提交数据时,会需要记录当前日期,等等。下面就看看一些 常用的方法 。

首先,datetime是一个struct。很多时候,会把它当成一个类。但它真的不是,msdn上的描述如下:

datetime结构: 表示时间上的一刻,通常以日期和当天的时间表示。语法:

?

[serializableattribute]

public struct datetime : icomparable, iformattable,

   iconvertible, iserializable, icomparable<datetime>, iequatable<datetime>

一、datetime.now属性

实例化一个datetime对象,可以将指定的数字作为年月日得到一个datetime对象。而datetime.now属性则可获得当前时间。如果你想按年、月、日分别统计数据,也可用datetime.now.year, datetime.now.month, datetime.now.day获取。同理,当前的时分秒也可以这样的方式获取。还可以在当前时间加上一个段时间等操作。    

?

static void main( string [] args)

     {

       datetime newchina = new datetime(1949, 10, 1);

       console.writeline(newchina);

       console.writeline( "当前时间:" );

       console.writeline( "{0}年,{1}月,{2}日" ,datetime.now.year, datetime.now.month, datetime.now.day);

       console.writeline( "{0}时,{1}分, {2}秒" ,datetime.now.hour, datetime.now.minute, datetime.now.second);

       console.writeline( "三天后:{0}" ,datetime.now.adddays(3));

       console.readline();

     }

结果:

二、tostring方法

 datetime的tostring方法有四种重载方式。其中一个重载方式允许传入string,这就意味着你可以将当前datetime对象转换成等效的字符串形式。比如我们将当前时间输出,日期按yyyy-mm-dd格式,时间按hh:mm:ss格式。

?

console.writeline(datetime.now.tostring( "yyyy-mm-dd" ));

   console.writeline(datetime.now.tostring( "hh:mm:ss" ));

还有一个重载形式是需要提供iformatprovider,使用指定的区域性特定格式信息将当前 datetime 对象的值转换为它的等效字符串表示形式。

?

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();

     }

结果:

三、daysinmonth方法及isleapyear方法

 daysinmonth方法需要两个int32型参数,返回指定年份指定月份的天数。关于月份的天数,多数只有2月需要特殊照顾一下。剩余的月份,无论哪一年的天数都是固定的。而二月呢,不但不是其他月份的30天或31天,她还分个闰年非闰年。

?

static void main( string [] args)

     {

       console.writeline( "2000年至2015年中二月的天数" );

       for ( int i = 2000; i < 2015; i++)

       {

         console.writeline( "{0}年2月有:{1}天" , i, datetime.daysinmonth(i, 2));

       }

       console.readline();

     }

输出结果:

从输出结果中可以看出,2月为29天的年份为闰年。但其实datetime还提供了判断闰年的方法isleapyear,该方法只要一个int32的参数,若输入的年份是闰年返回true,否则返回false。(.net framework就是这么贴心,你要的东西都给你封装好了,直接拿来用好了。)要是没这个方法呢,得自己去按照闰年的规则去写个小方法来判断。

?

static void main( string [] args)

     {

       console.writeline( "2000年至2015年中二月的天数" );

       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();

     }

微软现在已经将.netframework开源了,这意味着可以自己去查看源代码了。附上datetime.cs的源码链接,以及isleapyear方法的源代码。虽然仅仅两三行代码,但在实际开发中,你可能一时间想不起闰年的计算公式,或者拿捏不准。封装好的方法为你节省大量时间。

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);

  }

文章中介绍了几个算是比较常用的方法,希望对大家的学习有所帮助,平时多阅读相关文章,积累经验。

dy("nrwz");

查看更多关于C#中比较常用的DateTime结构的使用方法的详细内容...

  阅读:82次