DateTime 构造函数
l 初始化为指定的刻度数 public DateTime(long);
l 初始化为指定的年、月和日。 public DateTime(int, int, int);
l 初始化为指定 Calendar 的指定年、月和日。 public DateTime(int, int, int, Calendar);
l 初始化为指定的年、月、日、小时、分钟和秒。 public DateTime(int, int, int, int, int, int);
l 初始化为指定 Calendar 的指定年、月、日、小时、分钟和秒。 public DateTime(int, int, int, int, int, int, Calendar);
l 初始化为指定的年、月、日、小时、分钟、秒和毫秒。 public DateTime(int, int, int, int, int, int, int);
l 初始化为指定 Calendar 的指定年、月、日、小时、分钟、秒和毫秒。
public DateTime(int, int, int, int, int, int, int, Calendar);
Add 方法 | 将指定的 TimeSpan 的值加到此实例的值上 |
| public DateTime Add( TimeSpan value); value TimeSpan,它包含要添加的间隔。 DateTime,其值是此实例所表示的日期和时间与 value 所表示的时间间隔之和 ArgumentOutOfRangeException{得到的 DateTime 小于 MinValue 或大于 MaxValue} |
参数 | |
返回值 | |
异常 | |
| 此方法不更改此 DateTime 的值。而是返回一个新的 DateTime,其值是此运算的结果 |
示例 | 下面的说明 Add 方法。它计算距此刻为 36 天(即 864 小时)的那一天是星期几。 System.DateTime today = System.DateTime.Now; System.TimeSpan duration = new System.TimeSpan(36, 0, 0, 0); System.DateTime answer = today.Add(duration); |
AddDays 方法 | 将指定的天数加到此实例的值上 |
| public DateTime AddDays(double value); 由整数和小数部分组成的天数。value 参数可以是负数也可以是正数 其值是此实例所表示的日期和时间与 value 所表示的天数之和ArgumentOutOfRangeException{得到的 DateTime 小于 MinValue 或大于 MaxValue} |
参数 | |
返回值 | |
异常 | |
| value 的小数部分是以天为单位时的小数部分。例如,4.5 等效于 4 天 12 小时 |
AddHours方法 | 将指定的小时数加到此实例的值上 |
| public DateTime AddHours(double value); 由整数和小数部分组成的小时数。value 参数可以是负数也可以是正数 DateTime,其值是此实例所表示的日期和时间与 value 所表示的小时数之和ArgumentOutOfRangeException{得到的 DateTime 小于 MinValue 或大于 MaxValue} |
参数 | |
返回值 | |
异常 | |
| value 的小数部分是以天为单位时的小数部分。例如,4.5 等效于 4 天 12 小时 |
AddMilliseconds方法 | 将指定的毫秒数加到此实例的值上 |
| public DateTime AddMilliseconds (double value); 由整数和小数部分组成的小时数。value 参数可以是负数也可以是正数 DateTime,其值是此实例所表示的日期和时间与 value 所表示的毫秒数之和ArgumentOutOfRangeException{得到的 DateTime 小于 MinValue 或大于 MaxValue} |
参数 | |
返回值 | |
异常 | |
| value 的小数部分是以天为单位时的小数部分。例如,4.5 等效于 4 天 12 小时 |
AddMinutes方法 | 将指定的分钟数加到此实例的值上 |
| public DateTime AddMinutes (double value); 由整数和小数部分组成的小时数。value 参数可以是负数也可以是正数 DateTime,其值是此实例所表示的日期和时间与 value 所表示的分钟数之和ArgumentOutOfRangeException{得到的 DateTime 小于 MinValue 或大于 MaxValue} |
参数 | |
返回值 | |
异常 | |
| value 的小数部分是以天为单位时的小数部分。例如,4.5 等效于 4 天 12 小时 |
AddMonths方法 | 将指定的月份数加到此实例的值上 |
| public DateTime AddMonths (double value); 参数可以是负数也可以是正数 DateTime,其值是此实例所表示的日期和时间与 months 之和。ArgumentOutOfRangeException{得到的 DateTime 小于 MinValue 或大于 MaxValue。 - 或 - months 小于 -120,000 或大于 120,000。} |
参数 | |
返回值 | |
异常 |
AddSeconds方法 | 将指定的秒数加到此实例的值上 |
| public DateTime AddSeconds (double value); 参数可以是负数也可以是正数 DateTime,其值是此实例所表示的日期和时间与 value 所表示的秒数之和ArgumentOutOfRangeException{得到的 DateTime 小于 MinValue 或大于 MaxValue。 |
参数 | |
返回值 | |
异常 |
AddTicks方法 | 将指定的刻度数加到此实例的值上 |
| public DateTime AddTicks (double value); 以 100 毫微秒为单位的刻度数。value 参数可以是正数也可以是负数 DateTime,其值是此实例所表示的日期和时间与 value 所表示时间之和ArgumentOutOfRangeException{得到的 DateTime 小于 MinValue 或大于 MaxValue。 |
参数 | |
返回值 | |
异常 |
AddYears方法 | 将指定的年份数加到此实例的值上 |
| public DateTime AddYears (double value); 数可以是负数也可以是正数 DateTime,其值是此实例所表示的日期和时间与 value 所表示的年份数之和ArgumentOutOfRangeException{得到的 DateTime 小于 MinValue 或大于 MaxValue。 |
参数 | |
返回值 | |
异常 |
Compare方法 | 比较 DateTime 的两个实例,并返回它们相对值的指示 |
| public static int Compare( DateTime t1, DateTime t2 ); t1 第一个 DateTime。 t2 第二个 DateTime。 表示 t1 和 t2 的相对值的有符号的数字。 值类型条件 小于零t1 小于 t2。零t1 等于 t2。大于零t1 大于 t2。 |
参数 | |
返回值 | |
| DateTime t1 = new DateTime(100); DateTime t2 = new DateTime(20); if (DateTime.Compare(t1, t2) > 0) Console.WriteLine("t1 > t2"); if (DateTime.Compare(t1, t2) == 0) Console.WriteLine("t1 == t2"); if (DateTime.Compare(t1, t2) < 0) Console.WriteLine("t1 < t2"); |
CompareTo方法 | 将此实例与指定对象进行比较并返回一个对二者的相对值的指示 | |
| public virtual int CompareTo(object value); 要比较的对象,或为空引用(Visual Basic 中为 Nothing) 指示此实例与 value 的相对值 | |
参数 | ||
返回值 | ||
异常 | ArgumentException{ value 不是 DateTime} | |
| System.DateTime theDay = new System.DateTime(System.DateTime.Today.Year, 7, 28); int compareValue; try { compareValue = theDay.CompareTo(System.DateTime.Today);} catch (ArgumentException) { System.Console.WriteLine("Value is not a DateTime"); return;} if (compareValue < 0) { System.Console.WriteLine("{0:d} is in the past.", theDay);} else if (compareValue == 0) { System.Console.WriteLine("{0:d} is today!", theDay);} else if (compareValue == 1) { System.Console.WriteLine("Value is null");} // compareValue > 0 && compareValue != 1 else { System.Console.WriteLine("{0:d} has not come yet.", theDay);} | |
| |
| public DateTime Date {get;} 其日期与此实例相同,时间值设置为午夜 12 点 (00:00:00) |
Day 属性 | 获取此实例所表示的日期为该月中的第几天 |
| public int Day {get;} 日期值(介于 1 和 31 之间) |
属性值 | |
| System.DateTime moment = new System.DateTime(1999, 1, 13, 3, 57, 32, 11); int year = moment.Year; // Year gets 1999. int month = moment.Month; // Month gets 1 (January). int day = moment.Day; // Day gets 13. int hour = moment.Hour; // Hour gets 3. int minute = moment.Minute; // Minute gets 57. int second = moment.Second; // Second gets 32. int millisecond = moment.Millisecond; // Millisecond gets 11. |
DayOfWeek | 获取此实例所表示的日期是星期几 |
| public DayOfWeek DayOfWeek {get;} 一个 DayOfWeek 枚举常数,它指示星期几。该属性值的范围从零(表示星期日)到六(表示星期六) |
|
指定一周的某天 | |
| public enum DayOfWeek 在每周有七天的日历中,DayOfWeek 枚举表示一周的某天。该枚举的范围从零(表示星期日)到六(表示星期六)。当最好是对一周的某天进行强类型指定时,该枚举很有用。例如,该枚举是 DateTime.DayOfWeek 属性的类型 Friday 表示星期五 Monday 表示星期一 Saturday 表示星期六 Sunday表示星期日 Thursday 表示星期四 Tuesday 表示星期二 Wednesday 表示星期三 |
|
获取此实例所表示的日期是该年中的第几天 | |
| public int DayOfYear {get;} 该年中的第几天(介于 1 和 366 之间) |
|
返回指定年份中指定月份的天数 | |
| public static int DaysInMonth( ); 指定 year 中 month 的天数。 例如,如果 month 等于 2(表示二月),则返回值为 28 或 29,具体取决于 year 是否为闰年。1 |
| |
异常类型 | ArgumentOutOfRangeException{ month 小于 1 或大于 12} |
| const int July = 7; const int Feb = 2; int daysInJuly = System.DateTime.DaysInMonth(2001, July); // daysInJuly gets 31. // daysInFeb gets 28 because the year 1998 was not a leap year. int daysInFeb = System.DateTime.DaysInMonth(1998, Feb); // daysInFebLeap gets 29 because the year 1996 was a leap year. int daysInFebLeap = System.DateTime.DaysInMonth(1996, Feb); |
Equals | 返回一个值,该值指示 DateTime 的一个实例是否与指定对象相等 |
| public override bool Equals(object);public override bool Equals(object); |
System.DateTime today1 = new System.DateTime(System.DateTime.Today.Ticks); System.DateTime today2 = new System.DateTime(System.DateTime.Today.Ticks); System.DateTime tomorrow = new System.DateTime(System.DateTime.Today.AddDays(1).Ticks); // todayEqualsToday gets true. bool todayEqualsToday = System.DateTime.Equals(today1, today2); // todayEqualsTomorrow gets false. bool todayEqualsTomorrow = System.DateTime.Equals(today1, tomorrow); |
FromFileTime | 返回与指定的操作系统文件时间戳等效的 DateTime |
| public static DateTime FromFileTime(long fileTime); fileTime Windows 文件时间 |
异常类型 | |
备注 | fileTime 为表示 Windows 文件时间戳的 64 位有符号整数值。时间戳是自协调通用时间 (UTC) 公元 (C.E.) 1601 年 1 月 1 日午夜 12:00 以来所经过的时间以 100 毫微秒为间隔表示时的数字。 |
public System.TimeSpan FileAge(long fileCreationTime) { System.DateTime now = System.DateTime.Now; try { System.DateTime fCreationTime = System.DateTime.FromFileTime(fileCreationTime); System.TimeSpan fileAge = now.Subtract(fCreationTime); return fileAge; } catch (ArgumentOutOfRangeException) { // fileCreationTime is not valid, so re-throw the exception. throw; }} |
FromFileTimeUtc | 返回与指定的操作系统文件时间戳等效的 DateTime,已调整为协调通用时间 (UTC) |
| public: static DateTime FromFileTimeUtc( fileTime Windows 文件时间 |
| |
异常类型 | |
备注 | fileTime 为表示 Windows 文件时间戳的 64 位有符号整数值。时间戳是自协调通用时间 (UTC) 公元 (C.E.) 1601 年 1 月 1 日午夜 12:00 以来所经过的时间以 100 毫微秒为间隔表示时的数字。FromFileTimeUtc 方法返回相对于公元 (C.E.) 1 年 1 月 1 日午夜 12:00 的 fileTime |
Hour | 获取此实例所表示日期的小时部分 |
| public int Hour {get;} 小时数(介于 0 和 23 之间) |
IsLeapYear | 返回指定的年份是否为闰年的指示 |
| public static bool IsLeapYear( 如果 year 为闰年,则为 true;否则为 false |
|
表示 DateTime 可能的最大值。此字段为只读 | |
| public static readonly DateTime MaxValue; 此常数的值等效于 9999 年 12 月 31 日 23:59:59.9999999,恰好在 10000 年 1 月 1 日 00:00:00 之前一个 100 毫微秒刻度处 |
|
获取此实例所表示日期的毫秒部分 | |
| public int Millisecond {get;} |
Minute | 获取此实例所表示日期的毫秒部分 |
| public int Minute {get;} 分钟数(介于 0 和 59 之间) |
MinValue | 表示 DateTime 可能的最小值。此字段为只读 |
| public static readonly DateTime MinValue; 此常数的值等效于 0001 年 1 月 1 日 00:00:00.0000000 |
Month | 获取此实例所表示日期的月份部分 |
| public int Month {get;} 月份(介于 1 和 12 之间) |
Now | 获取一个 DateTime,它是此计算机上的当前本地日期和时间 |
| public static DateTime Now {get;} |
Parse | 将日期和时间的指定字符串表示转换成其等效的 DateTime |
| public static DateTime Parse(string); public static DateTime Parse(string, IFormatProvider); public static DateTime Parse(string, IFormatProvider, DateTimeStyles); |
string strMyDateTime = " 2/16/1992 12:15:12"; // myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec. System.DateTime myDateTime =System.DateTime.Parse(strMyDateTime); System.IFormatProvider format =new System.Globalization.CultureInfo("fr-FR", true); // Reverse month and day to conform to a different format. string strMyDateTimeFrench = " 16/02/1992 12:15:12"; // myDateTimeFrench gets Feburary 16, 1992, 12 hours, // 15 min and 12 sec. System.DateTime myDateTimeFrench = System.DateTime.Parse(strMyDateTimeFrench, format, System.Globalization. DateTimeStyles.NoCurrentDateDefault); string[] expectedFormats = {"G", "g", "f" ,"F"}; // myDateTimeFrench gets Feburary 16, 1992, 12 hours, // 15 min and 12 sec. myDateTimeFrench = System.DateTime.ParseExact(strMyDateTimeFrench, expectedFormats, format, System.Globalization. DateTimeStyles.AllowWhiteSpaces);
|
Second | 获取此实例所表示日期的秒部分 |
| public int Second {get;} |
Subtract | 从此实例中减去指定的时间或持续时间 |
| |
System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0); System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0); System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0); // diff1 gets 185 days, 14 hours, and 47 minutes. System.TimeSpan diff1 = date2.Subtract(date1); // date4 gets 4/9/1996 5:55:00 PM . System.DateTime date4 = date3.Subtract(diff1); // diff2 gets 55 days 4 hours and 20 minutes. System.TimeSpan diff2 = date2 - date3; // date5 gets 4/9/1996 5:55:00 PM . System.DateTime date5 = date1 - diff2; |
获取表示此实例的日期和时间的刻度数 | |
| public long Ticks {get;} 表示此实例的日期和时间的刻度数。该值介于 MinValue 和 MaxValue 之间 此属性的值为自 0001 年 1 月 1 日午夜 12:00 以来所经过时间以 100 毫微秒为间隔表示时的数字 |
TimeOfDay | 获取此实例的当天的时间 |
| public TimeSpan TimeOfDay {get;} TimeSpan,它表示当天自午夜以来已经过时间的部分 |
获取当前日期 | |
| public static DateTime Today {get;} DateTime,它设置为此实例的日期,时间部分设置为 00: 00:00 |
将此实例的值转换为采用本地操作系统文件时间的格式 | |
| public long ToFileTime(); |
异常类型 | ArgumentOutOfRangeException{此实例的值无法转换为系统文件时间} |
系统文件时间是表示日期和时间的 64 位无符号值,该值为自 1601 年 1 月 1 日午夜 12:00 以来所经过时间以 100 毫微秒为间隔表示时的数字。 如果试图转换协调通用时间 (UTC) 1601 年 1 月 1 日午夜 12:00 之前的日期,则会引发异常。 |
ToFileTimeUtc | 将此实例的值转换为操作系统文件时间的格式,而不考虑本地时区 |
| public static DateTime Today {get;} DateTime,它设置为此实例的日期,时间部分设置为 00: 00:00 |
异常类型 | ArgumentOutOfRangeException{生成的文件时间将小于 0} |
文件时间是一个表示 Windows 文件时间戳的 64 位有符号整数值。时间戳是自协调通用时间 (UTC) 公元 (C.E.) 1601 年 1 月 1 日午夜 12:00 以来所经过的时间以 100 毫微秒为间隔表示时的数字。ToFileTimeUtc 方法返回相对于公元 1601 (C.E.) 1 月 1 日午夜 12:00 的此 DateTime 的值,但是不会考虑本地时区对文件时间进行调整 |
ToLocalTime | 将此实例的值转换为操作系统文件时间的格式,而不考虑本地时区 |
| public static DateTime Today {get;} DateTime,它设置为此实例的日期,时间部分设置为 00: 00:00 |
异常类型 | ArgumentOutOfRangeException{生成的文件时间将小于 0} |
文件时间是一个表示 Windows 文件时间戳的 64 位有符号整数值。时间戳是自协调通用时间 (UTC) 公元 (C.E.) 1601 年 1 月 1 日午夜 12:00 以来所经过的时间以 100 毫微秒为间隔表示时的数字。ToFileTimeUtc 方法返回相对于公元 1601 (C.E.) 1 月 1 日午夜 12:00 的此 DateTime 的值,但是不会考虑本地时区对文件时间进行调整 |
ToFileTime | 将此实例的值转换为采用本地操作系统文件时间的格式 |
| public long ToFileTime(); |
异常类型 | ArgumentOutOfRangeException{此实例的值无法转换为系统文件时间} |
系统文件时间是表示日期和时间的 64 位无符号值,该值为自 1601 年 1 月 1 日午夜 12:00 以来所经过时间以 100 毫微秒为间隔表示时的数字。 如果试图转换协调通用时间 (UTC) 1601 年 1 月 1 日午夜 12:00 之前的日期,则会引发异常。 |
将此实例的值转换为其等效的长日期字符串表示形式 | |
| public string ToLongDateString(); |
| 一个字符串,它包含与此实例的日期值等效的日期为星期几的名称、月份名称、该月中的数字日期以及年份 此实例的值使用长日期格式字符“D”进行格式化。返回值与 ToString ("D", null) 返回的值相同。 有关格式字符、格式模式及它们产生的输出的更多信息,请参见格式化概述主题。有关更改与格式字符相关的格式模式的更多信息,请参见 DateTimeFormatInfo 类。 此方法使用从当前区域性导出的格式信息。有关当前区域性的更多信息,请参见 CurrentCulture 类。可使用 CultureInfo.DateTimeFormat 属性获取当前区域性的 DateTimeFormatInfo。 |
System.DateTime dTime = new System.DateTime(2001, 5, 16, 3, 2, 15); // If the current culture is "us-EN", // longDateString gets " Wednesday, May 16, 2001 " string longDateString = dTime.ToLongDateString(); // If the current culture is "us-EN", // longTimeString gets " 3:02:15 AM " string longTimeString = dTime.ToLongTimeString(); |
ToLongTimeString | 将此实例的值转换为其等效的长时间字符串表示形式 |
| public string ToLongTimeString(); |
| 与此实例中的时间值等效的一个字符串,其中包含当日的周日名称、当月的名称以及小时、分和秒的数字日期。 此实例的值使用长时间格式字符“T”进行格式化。返回值与 ToString ("T", null) 返回的值相同。 有关格式字符、格式模式及它们产生的输出的更多信息,请参见格式化概述主题。有关更改与格式字符相关的格式模式的更多信息,请参见 DateTimeFormatInfo 类。 |
ToShortDateString | 将此实例的值转换为其等效的短日期字符串表示形式 |
| public string ToShortDateString(); |
| 将此实例的值转换为其等效的短日期字符串表示形式。 一个字符串,它包含与此实例的日期值等效的数字月份、该月中的数字日期和年份。 此实例的值使用短日期格式字符“d”进行格式化。返回值与 ToString ("d", null) 返回的值相同。 |
将此实例的值转换为其等效的短时间字符串表示形式 | |
| public string ToShortTimeString(); |
| 与此实例中的时间值等效的一个字符串,其中包含当日的周日名称、当月的名称以及小时、分和秒的数字日期。 此实例的值使用短时间格式字符“t”进行格式化。返回值与 ToString ("t", null) 返回的值相同。 |
将当前本地时间转换为协调通用时间 (UTC) | |
| public DateTime ToUniversalTime(); |
| 等效于当前本地时间的 UTC DateTime。 - 或 - 果当前本地时间太大,无法表示为 DateTime,则为 MaxValue。 - 或 - 果当前本地时间太小,无法表示为 DateTime,则为 MinValue。 UTC 时间等于本地时间减去 UTC 偏移量。有关 UTC 偏移量的更多信息,请参见 TimeZone.GetUtcOffset。 此方法假定当前 DateTime 保存本地时间值而不是 UTC 时间。因此,每次它运行时,当前方法均对 DateTime 执行必要的修改以导出 UTC 时间,而不管当前 DateTime 保存的是否为本地时间。此方法在进行计算时总是使用本地时区。
|
UtcNow | 获取一个 DateTime,它是以协调通用时间 (UTC) 表示的此计算机上的当前本地日期和时间 | ||||||
| public static DateTime UtcNow {get;} | ||||||
| 获取一个 DateTime,它是以协调通用时间 (UTC) 表示的此计算机上的当前本地日期和时间。 public static DateTime UtcNow {get;} 属性值:其值为当前 UTC 日期和时间的 DateTime。 备注:此属性的分辨率取决于系统计时器。
|