.net中对时间的操作

DateTime相关属性

        DateTime 日期=DateTime.Today;                   //'Today'获取当前日期
        Response.Write(日期);
        //显示2010-1-13 0:00:00
        DateTime dt = DateTime.Now;                     //'Now'获取当前时间包括年月日时分秒
        Response.Write(dt.ToString());
        //显示2010-1-13 20:36:12
        Response.Write(dt.Date.ToString());             //'Date'获取当前实例dt的日期部分
        //显示2010-1-13 0:00:00
        Response.Write(dt.Day.ToString());              //'Day'获取当前实例dt所表示是的日期为该月中的第几天,即日
        //显示13
        Response.Write(dt.DayOfWeek.ToString());        //'DayOfWeek'获取当前实例dt所表示的日期是星期几
        //显示Wednesday
        Response.Write(dt.DayOfYear.ToString());        //'DayOfYear'获取当前实例dt所表示的日期是一年中的第几天
        //显示13
        Response.Write(dt.Hour.ToString());             //'Hour'获取当前实例dt所表示日期的小时部分
        //显示20
        Response.Write(dt.Millisecond.ToString());      //'Milliscond'获取当前实例dt所表示日期的毫秒部分
        //显示687
        Response.Write(dt.Minute.ToString());           //'Minute'获取当前实例dt所表示日期的分钟部分
        //显示36
        Response.Write(dt.Month.ToString());            //'Month'获取当前实例dt所表示日期的月份
        //显示1
        Response.Write(dt.Second.ToString());           //'Second'获取当前实例dt所表示日期的秒
        //显示12
        Response.Write(dt.TimeOfDay.ToString());        //'TimeOfDay'获取当前实例dt所表示日期的时间部分
        //显示20:36:12.6875000
        Response.Write(dt.Year.ToString());             //'Year'获取当前实例dt所表示日期的的年份
        //显示2010

DateTime相关方法:

1.Add方法:将指定的TimeSpan的值加到实例的值上

public DateTime Add (
 TimeSpan value
) 参数TimeSpan包含要添加的时间间隔; 对象表示时间间隔或持续时间,按正负天数、小时数、分钟数、秒数以及秒的小数部分进行度量。如: DateTime dt = DateTime.Now;        TimeSpan ts = new TimeSpan(1, 1, 1, 1);//参数表示1天,1小时,1分钟,1秒,1毫秒        Response.Write(dt.Add(ts).ToString());2.AddDays方法:将指定的天数加到实例的值上public DateTime AddDays (
 double value
)
 
如:DateTime dt = DateTime.Now;        Response.Write(dt.AddDays(30).ToString());//多余的部分按顺序加到前面的单位
 
3.AddHours:将指定的小时加到实例的值上
 
4.AddMilliseconds:将指定的毫秒数加到实例的值上
 
5.AddMinute:同上
 
6.AddMonths:同上
 
7.AddSeconds:同上
 
8.AddYears:同上
 
9.Compare:比较DateTime的两个实例,并返回他们的相对值的指示。
public static int Compare (
 DateTime t1,
 DateTime t2
)
实例如下: DateTime dt1 = new DateTime(100);        DateTime dt2 = new DateTime(200);        if (DateTime.Compare(dt1, dt2) > 0)            Response.Write("dt1 > dt2");        if (DateTime.Compare(dt1, dt2) == 0)            Response.Write("dt1=dt2");        if (DateTime.Compare(dt1, dt2) < 0)            Response.Write("dt1 < dt2");
 
10.CompareTo:将此实例与指定对象进行比较并返回一个对二者的相对值(int)的指示。
如:DateTime dt1 = new DateTime(100);        DateTime dt2 = new DateTime(200);        int i=dt1.CompareTo(dt2);        Response.Write(i);
11.DaysInMonth:返回指定年和月中的天数。
如:int i = DateTime.DaysInMonth(2010, 1);        Response.Write(i);
12.Equals方法:返回一个值,该值指示此实例是否与指定的 DateTime 实例相等。返回bool(true或false)
如: DateTime dt1 = DateTime.Now.AddDays(1);        DateTime dt2 = DateTime.Now;        Response.Write(dt1.Equals(dt2));
13.ToLongDateString 将此实例的值转换为其等效的长日期字符串表示形式。
14.ToLongTimeString 将此实例的值转换为其等效的长时间字符串表示形式。
15.ToShortDateString 将此实例的值转换为其等效的短日期字符串表示形式。
16.ToShortTimeString 将此实例的值转换为其等效的短时间字符串表示形式。


本文来自CSDN博客,出处:http://blog.csdn.net/bestxulei/archive/2010/01/13/5186773.aspx

原文地址:https://www.cnblogs.com/jxcia_Lai/p/1749321.html