[Serializable] |
Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 1 C.E. in the GregorianCalendar calendar. For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight. A DateTime value is always expressed in the context of an explicit or default calendar.
The DateTime and TimeSpan value types differ in that a DateTime represents an instant in time, whereas a TimeSpan represents a time interval. This means, for example, that you can subtract one instance of DateTime from another to obtain the time interval between them. Or you could add a positive TimeSpan to the current DateTime to calculate a future date.
Time values can be added to, or subtracted from, an instance of DateTime. Time values can be negative or positive, and expressed in units such as ticks, seconds, or instances of TimeSpan. Methods and properties in this value type take into account details such as leap years and the number of days in a month.
Descriptions of time values in this type are often expressed using the coordinated universal time (UTC) standard, which was previously known as Greenwich mean time (GMT).
Calculations and comparisons of DateTime instances are only meaningful when the instances are created in the same time zone. For that reason, it is assumed that the developer has some external mechanism, such as an explicit variable or policy, to know in which time zone a DateTime was created. Methods and properties in this class always use the local time zone when making calculations or comparisons.
A calculation on an instance of DateTime, such as DateTime.Add or DateTime.Subtract, does not modify the value of the instance. Instead, the calculation returns a new instance of DateTime whose value is the result of the calculation.
This type inherits from IComparable, IFormattable, and IConvertible. Use the Convert class for conversions instead of this type's explicit interface member implementation of IConvertible.
ctor #1 | Overloaded:.ctor(long ticks) Initializes a new instance of the DateTime structure to a specified number of ticks. |
ctor #2 | Overloaded:.ctor(int year, int month, int day) Initializes a new instance of the DateTime structure to the specified year, month, and day. |
ctor #3 | Overloaded:.ctor(int year, int month, int day, Calendar calendar) Initializes a new instance of the DateTime structure to the specified year, month, and day for the specified Calendar. |
ctor #4 | Overloaded:.ctor(int year, int month, int day, int hour, int minute, int second) Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second. |
ctor #5 | Overloaded:.ctor(int year, int month, int day, int hour, int minute, int second, Calendar calendar) Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second for the specified Calendar. |
ctor #6 | Overloaded:.ctor(int year, int month, int day, int hour, int minute, int second, int millisecond) Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond. |
ctor #7 | Overloaded:.ctor(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar) Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond for the specified Calendar. |
MaxValue | Represents the largest possible value of DateTime. This field is read-only. |
MinValue | Represents the smallest possible value of DateTime. This field is read-only. |
Date | Read-only Gets the date component of this instance. |
Day | Read-only Gets the day of the month represented by this instance. |
DayOfWeek | Read-only Gets the day of the week represented by this instance. |
DayOfYear | Read-only Gets the day of the year represented by this instance. |
Hour | Read-only Gets the hour component of the date represented by this instance. |
Millisecond | Read-only Gets the milliseconds component of the date represented by this instance. |
Minute | Read-only Gets the minute component of the date represented by this instance. |
Month | Read-only Gets the month component of the date represented by this instance. |
Now | Read-only Gets a DateTime that is the current local date and time on this computer. |
Second | Read-only Gets the seconds component of the date represented by this instance. |
Ticks | Read-only Gets the number of ticks that represent the date and time of this instance. |
TimeOfDay | Read-only Gets the time of day for this instance. |
Today | Read-only Gets the current date. |
UtcNow | Read-only Gets a DateTime that is the current local date and time on this computer expressed as the coordinated universal time (UTC). |
Year | Read-only Gets the year component of the date represented by this instance. |
Add | Adds the value of the specified TimeSpan to the value of this instance. |
AddDays | Adds the specified number of days to the value of this instance. |
AddHours | Adds the specified number of hours to the value of this instance. |
AddMilliseconds | Adds the specified number of milliseconds to the value of this instance. |
AddMinutes | Adds the specified number of minutes to the value of this instance. |
AddMonths | Adds the specified number of months to the value of this instance. |
AddSeconds | Adds the specified number of seconds to the value of this instance. |
AddTicks | Adds the specified number of ticks to the value of this instance. |
AddYears | Adds the specified number of years to the value of this instance. |
Compare | Compares two instances of DateTime and returns an indication of their relative values. |
CompareTo | Compares this instance to a specified object and returns an indication of their relative values. |
DaysInMonth | Returns the number of days in the specified month of the specified year. |
Equals | Overloaded:Equals(object value) Overridden: Returns a value indicating whether this instance is equal to a specified object. |
Equals | Overloaded:Equals(DateTime t1, DateTime t2) Returns a value indicating whether two instances of DateTime are equal. |
FromFileTime | Returns a DateTime equivalent to the specified operating system file timestamp. |
FromOADate | Returns a DateTime equivalent to the specified OLE Automation Date. |
GetDateTimeFormats | Overloaded:GetDateTimeFormats() Converts the value of this instance to all the string representations supported by the standard DateTime format specifiers. |
GetDateTimeFormats | Overloaded:GetDateTimeFormats(char format) Converts the value of this instance to all the string representations supported by the specified standard DateTime format specifier. |
GetDateTimeFormats | Overloaded:GetDateTimeFormats(IFormatProvider provider) Converts the value of this instance to all the string representations supported by the standard DateTime format specifiers and the specified culture-specific formatting information. |
GetDateTimeFormats | Overloaded:GetDateTimeFormats(char format, IFormatProvider provider) Converts the value of this instance to all the string representations supported by the specified standard DateTime format specifier and culture-specific formatting information. |
GetHashCode | Overridden: Returns the hash code for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
GetTypeCode | Returns the TypeCode for value type DateTime. |
IsLeapYear | Returns an indication whether the specified year is a leap year. |
Parse | Overloaded:Parse(string s) Converts the specified string representation of a date and time to its DateTime equivalent. |
Parse | Overloaded:Parse(string s, IFormatProvider provider) Converts the specified string representation of a date and time to its DateTime equivalent using the specified culture-specific format information. |
Parse | Overloaded:Parse(string s, IFormatProvider provider, DateTimeStyles styles) Converts the specified string representation of a date and time to its DateTime equivalent using the specified culture-specific format information and formatting style. |
ParseExact | Overloaded:ParseExact(string s, string format, IFormatProvider provider) Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly. |
ParseExact | Overloaded:ParseExact(string s, string format, IFormatProvider provider, DateTimeStyles style) Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. |
ParseExact | Overloaded:ParseExact(string s, string[] formats, IFormatProvider provider, DateTimeStyles style) Converts the specified string representation of a date and time to its DateTime equivalent using the specified array of formats, culture-specific format information, and style. The format of the string representation must match at least one of the specified formats exactly. |
Subtract | Overloaded:Subtract(DateTime value) Subtracts the specified date and time from this instance. |
Subtract | Overloaded:Subtract(TimeSpan value) Subtracts the specified duration from this instance. |
ToFileTime | Converts the value of this instance to the format of the local system file time. |
ToLocalTime | Converts the current coordinated universal time (UTC) to local time. |
ToLongDateString | Converts the value of this instance to its equivalent long date string representation. |
ToLongTimeString | Converts the value of this instance to its equivalent long time string representation. |
ToOADate | Converts the value of this instance to the equivalent OLE Automation date. |
ToShortDateString | Converts the value of this instance to its equivalent short date string representation. |
ToShortTimeString | Converts the value of this instance to its equivalent short time string representation. |
ToString | Overloaded:ToString() Overridden: Converts the value of this instance to its equivalent string. |
ToString | Overloaded:ToString(IFormatProvider provider) Converts the value of this instance to its equivalent string representation using the specified culture-specific format information. |
ToString | Overloaded:ToString(string format) Converts the value of this instance to its equivalent string representation using the specified format. |
ToString | Overloaded:ToString(string format, IFormatProvider provider) Converts the value of this instance to its equivalent string representation using the specified format and culture-specific format information. |
ToUniversalTime | Converts the current local time to coordinated universal time (UTC). |
op_Addition | Adds a specified time interval to a specified date and time, yielding a new date and time. |
op_Equality | Determines whether two specified instances of DateTime are equal. |
op_GreaterThan | Determines whether one specified DateTime is greater than another specified DateTime. |
op_GreaterThanOrEqual | Determines whether one specified DateTime is greater than or equal to another specified DateTime. |
op_Inequality | Determines whether two specified instances of DateTime are not equal. |
op_LessThan | Determines whether one specified DateTime is less than another specified DateTime. |
op_LessThanOrEqual | Determines whether one specified DateTime is less than or equal to another specified DateTime. |
op_Subtraction | Overloaded:op_Subtraction Subtracts a specified date and time from another specified date and time, yielding a time interval. |
op_Subtraction | Overloaded:op_Subtraction Subtracts a specified time interval from a specified date and time, yielding a new date and time. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public DateTime( |
ticks
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | ticks is less than DateTime.MinValue or greater than DateTime.MaxValue. |
// Create a new instance of DateTime containing the date // 7/28/1979 at 10:35:05 PM using the en-US calendar. System.Globalization.CultureInfo info = new System.Globalization.CultureInfo("en-US", false); System.Globalization.Calendar calendar = info.Calendar; System.DateTime dateTime = new System.DateTime(1979, // Year 07, // Month 28, // Day 22, // Hour 35, // Minute 5, // Second 15, // Millisecond calendar // Calendar ); // Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM". System.Console.WriteLine("{0:F}", dateTime);
year
month
day
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | year is less than 1 or greater than 9999. -or- month is less than 1 or greater than 12. -or- day is less than 1 or greater than the number of days in month. |
ArgumentException | The specified parameters evaluate to less than DateTime.MinValue or more than DateTime.MaxValue. |
// Create a new instance of DateTime containing the date // 7/28/1979 at 10:35:05 PM using the en-US calendar. System.Globalization.CultureInfo info = new System.Globalization.CultureInfo("en-US", false); System.Globalization.Calendar calendar = info.Calendar; System.DateTime dateTime = new System.DateTime(1979, // Year 07, // Month 28, // Day 22, // Hour 35, // Minute 5, // Second 15, // Millisecond calendar // Calendar ); // Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM". System.Console.WriteLine("{0:F}", dateTime);
year
month
day
calendar
Exception Type | Condition |
---|---|
ArgumentNullException | calendar is null |
ArgumentOutOfRangeException | year is less than 1 or greater than 9999. -or- month is less than 1 or greater than the number of months in calendar. -or- day is less than 1 or greater than the number of days in month. |
ArgumentException | Specified parameters evaluate to less than DateTime.MinValue or more than DateTime.MaxValue. |
The allowable values for year, month, and day depend on the specified Calendar. An exception is thrown if the specified date and time cannot be expressed using calendar.
The System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.
// Create a new instance of DateTime containing the date // 7/28/1979 at 10:35:05 PM using the en-US calendar. System.Globalization.CultureInfo info = new System.Globalization.CultureInfo("en-US", false); System.Globalization.Calendar calendar = info.Calendar; System.DateTime dateTime = new System.DateTime(1979, // Year 07, // Month 28, // Day 22, // Hour 35, // Minute 5, // Second 15, // Millisecond calendar // Calendar ); // Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM". System.Console.WriteLine("{0:F}", dateTime);
year
month
day
hour
minute
second
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | year is less than 1 or greater than 9999 -or- month is less than 1 or greater than 12 -or- day is less than 1 or greater than the number of days in month -or- hour is less than 0 or greater than 23 -or- minute is less than 0 or greater than 59 -or- second is less than 0 or greater than 59 |
ArgumentException | Specified parameters evaluate to less than DateTime.MinValue or more than DateTime.MaxValue. |
// Create a new instance of DateTime containing the date // 7/28/1979 at 10:35:05 PM using the en-US calendar. System.Globalization.CultureInfo info = new System.Globalization.CultureInfo("en-US", false); System.Globalization.Calendar calendar = info.Calendar; System.DateTime dateTime = new System.DateTime(1979, // Year 07, // Month 28, // Day 22, // Hour 35, // Minute 5, // Second 15, // Millisecond calendar // Calendar ); // Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM". System.Console.WriteLine("{0:F}", dateTime);
public DateTime( |
year
month
day
hour
minute
second
calendar
Exception Type | Condition |
---|---|
ArgumentNullException | calendar is null. |
ArgumentOutOfRangeException | year is less than 1 or greater than 9999. -or- month is less than 1 or greater than the number of months in calendar. -or- day is less than 1 or greater than the number of days in month. -or- hour is less than 0 or greater than 23 -or- minute is less than 0 or greater than 59 -or- second is less than 0 or greater than 59 |
ArgumentException | Specified parameters evaluate to less than DateTime.MinValue or more than DateTime.MaxValue. |
The System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.
// Create a new instance of DateTime containing the date // 7/28/1979 at 10:35:05 PM using the en-US calendar. System.Globalization.CultureInfo info = new System.Globalization.CultureInfo("en-US", false); System.Globalization.Calendar calendar = info.Calendar; System.DateTime dateTime = new System.DateTime(1979, // Year 07, // Month 28, // Day 22, // Hour 35, // Minute 5, // Second 15, // Millisecond calendar // Calendar ); // Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM". System.Console.WriteLine("{0:F}", dateTime);
public DateTime( |
year
month
day
hour
minute
second
millisecond
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | year is less than 1 or greater than 9999. -or- month is less than 1 or greater than 12. -or- day is less than 1 or greater than the number of days in month. -or- hour is less than 0 or greater than 23. -or- minute is less than 0 or greater than 59. -or- second is less than 0 or greater than 59. |
ArgumentException | Specified parameters evaluate to less than DateTime.MinValue or more than DateTime.MaxValue. |
// Create a new instance of DateTime containing the date // 7/28/1979 at 10:35:05 PM using the en-US calendar. System.Globalization.CultureInfo info = new System.Globalization.CultureInfo("en-US", false); System.Globalization.Calendar calendar = info.Calendar; System.DateTime dateTime = new System.DateTime(1979, // Year 07, // Month 28, // Day 22, // Hour 35, // Minute 5, // Second 15, // Millisecond calendar // Calendar ); // Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM". System.Console.WriteLine("{0:F}", dateTime);
public DateTime( |
year
month
day
hour
minute
second
millisecond
calendar
Exception Type | Condition |
---|---|
ArgumentNullException | calendar is null. |
ArgumentOutOfRangeException | year is less than 1 or greater than 9999. -or- month is less than 1 or greater than the number of months in calendar. -or- day is less than 1 or greater than the number of days in month. -or- hour is less than 0 or greater than 23. -or- minute is less than 0 or greater than 59. -or- second is less than 0 or greater than 59. |
ArgumentException | Specified parameters evaluate to less than DateTime.MinValue or more than DateTime.MaxValue. |
The System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.
// Create a new instance of DateTime containing the date // 7/28/1979 at 10:35:05 PM using the en-US calendar. System.Globalization.CultureInfo info = new System.Globalization.CultureInfo("en-US", false); System.Globalization.Calendar calendar = info.Calendar; System.DateTime dateTime = new System.DateTime(1979, // Year 07, // Month 28, // Day 22, // Hour 35, // Minute 5, // Second 15, // Millisecond calendar // Calendar ); // Write the DateTime as "Saturday, July 28, 1979 10:35:05 PM". System.Console.WriteLine("{0:F}", dateTime);
public static readonly DateTime MaxValue;
|
public static readonly DateTime MinValue;
|
public DateTime Date {get;}
|
public int Day {get;}
|
System.DateTime moment = new System.DateTime( 1999, 1, 13, 3, 57, 32, 11); // Year gets 1999. int year = moment.Year; // Month gets 1 (January). int month = moment.Month; // Day gets 13. int day = moment.Day; // Hour gets 3. int hour = moment.Hour; // Minute gets 57. int minute = moment.Minute; // Second gets 32. int second = moment.Second; // Millisecond gets 11. int millisecond = moment.Millisecond;
public DayOfWeek DayOfWeek {get;}
|
public int DayOfYear {get;}
|
public int Hour {get;}
|
System.DateTime moment = new System.DateTime( 1999, 1, 13, 3, 57, 32, 11); // Year gets 1999. int year = moment.Year; // Month gets 1 (January). int month = moment.Month; // Day gets 13. int day = moment.Day; // Hour gets 3. int hour = moment.Hour; // Minute gets 57. int minute = moment.Minute; // Second gets 32. int second = moment.Second; // Millisecond gets 11. int millisecond = moment.Millisecond;
public int Millisecond {get;}
|
System.DateTime moment = new System.DateTime( 1999, 1, 13, 3, 57, 32, 11); // Year gets 1999. int year = moment.Year; // Month gets 1 (January). int month = moment.Month; // Day gets 13. int day = moment.Day; // Hour gets 3. int hour = moment.Hour; // Minute gets 57. int minute = moment.Minute; // Second gets 32. int second = moment.Second; // Millisecond gets 11. int millisecond = moment.Millisecond;
public int Minute {get;}
|
System.DateTime moment = new System.DateTime( 1999, 1, 13, 3, 57, 32, 11); // Year gets 1999. int year = moment.Year; // Month gets 1 (January). int month = moment.Month; // Day gets 13. int day = moment.Day; // Hour gets 3. int hour = moment.Hour; // Minute gets 57. int minute = moment.Minute; // Second gets 32. int second = moment.Second; // Millisecond gets 11. int millisecond = moment.Millisecond;
public int Month {get;}
|
System.DateTime moment = new System.DateTime( 1999, 1, 13, 3, 57, 32, 11); // Year gets 1999. int year = moment.Year; // Month gets 1 (January). int month = moment.Month; // Day gets 13. int day = moment.Day; // Hour gets 3. int hour = moment.Hour; // Minute gets 57. int minute = moment.Minute; // Second gets 32. int second = moment.Second; // Millisecond gets 11. int millisecond = moment.Millisecond;
public static DateTime Now {get;}
|
System | Approximate Resolution |
---|---|
Windows NT 3.5 and later | 10 milliseconds |
Windows NT 3.1 | 16 milliseconds |
Windows 98 | 55 milliseconds |
public int Second {get;}
|
System.DateTime moment = new System.DateTime( 1999, 1, 13, 3, 57, 32, 11); // Year gets 1999. int year = moment.Year; // Month gets 1 (January). int month = moment.Month; // Day gets 13. int day = moment.Day; // Hour gets 3. int hour = moment.Hour; // Minute gets 57. int minute = moment.Minute; // Second gets 32. int second = moment.Second; // Millisecond gets 11. int millisecond = moment.Millisecond;
public long Ticks {get;}
|
public TimeSpan TimeOfDay {get;}
|
public static DateTime Today {get;}
|
public static DateTime UtcNow {get;}
|
System | Approximate Resolution |
---|---|
Windows NT 3.5 and later | 10 milliseconds |
Windows NT 3.1 | 16 milliseconds |
Windows 98 | 55 milliseconds |
public int Year {get;}
|
System.DateTime moment = new System.DateTime( 1999, 1, 13, 3, 57, 32, 11); // Year gets 1999. int year = moment.Year; // Month gets 1 (January). int month = moment.Month; // Day gets 13. int day = moment.Day; // Hour gets 3. int hour = moment.Hour; // Minute gets 57. int minute = moment.Minute; // Second gets 32. int second = moment.Second; // Millisecond gets 11. int millisecond = moment.Millisecond;
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue. |
// Calculate what day of the week is 36 days from this instant. System.DateTime today = System.DateTime.Now; System.TimeSpan duration = new System.TimeSpan(36, 0, 0, 0); System.DateTime answer = today.Add(duration); System.Console.WriteLine("{0:dddd}", answer);
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue. |
The fractional part of value is the fractional part of a day. For example, 4.5 is equivalent to 4 days, 12 hours, 0 minutes, 0 seconds, 0 milliseconds, and 0 ticks.
The value parameter is rounded to the nearest tick.
// Calculate what day of the week is 36 days from this instant. System.DateTime today = System.DateTime.Now; System.DateTime answer = today.AddDays(36); System.Console.WriteLine("{0:dddd}", answer);
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue. |
The fractional part of value is the fractional part of an hour. For example, 4.5 is equivalent to 4 hours, 30 minutes, 0 seconds, 0 milliseconds, and 0 ticks.
The value parameter is rounded to the nearest tick.
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue. |
The fractional part of value is the fractional part of a millisecond. For example, 4.5 is equivalent to 4 milliseconds, and 5000 ticks (where one millisecond = 10000 ticks).
The value parameter is rounded to the nearest tick.
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue. |
The fractional part of value is the fractional part of a minute. For example, 4.5 is equivalent to 4 minutes, 30 seconds, 0 milliseconds, and 0 ticks.
The value parameter is rounded to the nearest tick.
months
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue. -or- months is less than -120,000 or greater than 120,000. |
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue. |
The fractional part of value is the fractional part of a second. For example, 4.5 is equivalent to 4 seconds, 500 milliseconds, and 0 ticks.
The value parameter is rounded to the nearest tick.
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue. |
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | value or the resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue. |
t1
t2
Value Type | Condition |
---|---|
Less than zero | is less than . |
Zero | equals . |
Greater than zero | is greater than . |
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");
value
Value | Description |
---|---|
Less than zero | This instance is less than . |
Zero | This instance is equal to . |
Greater than zero | This instance is greater than , or is null . |
Exception Type | Condition |
---|---|
ArgumentException | value is not a DateTime. |
value must be an instance of DateTime or null; otherwise, an exception is thrown.
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); }
year
month
For example, if month equals 2 for February, the return value is 28 or 29 depending upon whether year is a leap year.
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | month is less than 1 or greater than 12. |
const int July = 7; const int Feb = 2; // daysInJuly gets 31. int daysInJuly = System.DateTime.DaysInMonth(2001, July); // 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);
value
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);
t1
t2
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);
~DateTime(); |
fileTime
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | fileTime is not valid. |
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; } }
d
Exception Type | Condition |
---|---|
ArgumentException | The date is not a valid OLE Automation Date value. |
See DateTime.ToOADate and the MSDN Online Library at http://MSDN.Microsoft.com/library/default.asp for more information on OLE Automation.
public string[] GetDateTimeFormats(); |
For more information about the standard formatting specifiers, see DateTimeFormatInfo.
DateTime july28 = new DateTime(1979, 7, 28, 5, 23, 15, 16); string[] july28Formats = july28.GetDateTimeFormats(); // Print out july28 in all DateTime formats using the // default culture. foreach (string format in july28Formats) { System.Console.WriteLine(format); } IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true); // Get the short date formats using the "fr-FR" culture. string [] frenchJuly28Formats = july28.GetDateTimeFormats('d', culture); // Print out july28 in various formats using "fr-FR" culture. System.Console.WriteLine("Starting fr-FR formats"); foreach (string format in frenchJuly28Formats) { System.Console.WriteLine(format); }
format
For more information about the standard formatting specifiers, see DateTimeFormatInfo.
DateTime july28 = new DateTime(1979, 7, 28, 5, 23, 15, 16); string[] july28Formats = july28.GetDateTimeFormats(); // Print out july28 in all DateTime formats using the // default culture. foreach (string format in july28Formats) { System.Console.WriteLine(format); } IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true); // Get the short date formats using the "fr-FR" culture. string [] frenchJuly28Formats = july28.GetDateTimeFormats('d', culture); // Print out july28 in various formats using "fr-FR" culture. System.Console.WriteLine("Starting fr-FR formats"); foreach (string format in frenchJuly28Formats) { System.Console.WriteLine(format); }
public string[] GetDateTimeFormats( |
provider
For more information about the standard formatting specifiers, see DateTimeFormatInfo.
DateTime july28 = new DateTime(1979, 7, 28, 5, 23, 15, 16); string[] july28Formats = july28.GetDateTimeFormats(); // Print out july28 in all DateTime formats using the // default culture. foreach (string format in july28Formats) { System.Console.WriteLine(format); } IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true); // Get the short date formats using the "fr-FR" culture. string [] frenchJuly28Formats = july28.GetDateTimeFormats('d', culture); // Print out july28 in various formats using "fr-FR" culture. System.Console.WriteLine("Starting fr-FR formats"); foreach (string format in frenchJuly28Formats) { System.Console.WriteLine(format); }
public string[] GetDateTimeFormats( |
format
provider
For more information about the standard formatting specifiers, see DateTimeFormatInfo.
DateTime july28 = new DateTime(1979, 7, 28, 5, 23, 15, 16); string[] july28Formats = july28.GetDateTimeFormats(); // Print out july28 in all DateTime formats using the // default culture. foreach (string format in july28Formats) { System.Console.WriteLine(format); } IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true); // Get the short date formats using the "fr-FR" culture. string [] frenchJuly28Formats = july28.GetDateTimeFormats('d', culture); // Print out july28 in various formats using "fr-FR" culture. System.Console.WriteLine("Starting fr-FR formats"); foreach (string format in frenchJuly28Formats) { System.Console.WriteLine(format); }
public override int GetHashCode(); |
public Type GetType(); |
public TypeCode GetTypeCode(); |
year
protected object MemberwiseClone(); |
d
t
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue. |
System.DateTime dTime = new System.DateTime(1980, 8, 5); // tSpan is 17 days, 4 hours, 2 minutes and 1 second. System.TimeSpan tSpan = new System.TimeSpan(17, 4, 2, 1); // Result gets 8/22/1980 4:02:01 AM. System.DateTime result = dTime + tSpan;
d1
d2
System.DateTime april19 = new DateTime(2001, 4, 19); System.DateTime otherDate = new DateTime(1991, 6, 5); // areEqual gets false. bool areEqual = april19 == otherDate; otherDate = new DateTime(2001, 4, 19); // areEqual gets true. areEqual = april19 == otherDate;
t1
t2
t1
t2
d1
d2
t1
t2
t1
t2
d1
d2
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;
d
t
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;
s
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
FormatException | s does not contain a valid string representation of a date and time. |
This method attempts to parse s completely and avoid throwing FormatException. It ignores unrecognized data if possible and fills in missing month, day, and year information with the current time. If s contains only a date and no time, this method assumes 12 A.M. Any leading, inner, or trailing white space character in s is ignored.
Parameter s must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic.
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);
public static DateTime Parse( |
s
provider
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
FormatException | s does not contain a valid string representation of a date and time. |
This method attempts to parse s completely and avoid throwing FormatException. It ignores unrecognized data if possible and fills in missing month, day, and year information with the current time. If s contains only a date and no time, this method assumes 12 A.M. Any leading, inner, or trailing white space character in s is ignored.
Parameter s must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic.
Parameter provider supplies culture-specific date and time formatting information. For example, the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. If provider is null, the current culture is used.
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);
public static DateTime Parse( |
s
provider
styles
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
FormatException | s does not contain a valid string representation of a date and time. |
This method attempts to ignore unrecognized data and parse s completely. If s contains a time but no date, the style parameter determines whether the current date or a default date is used. If s contains a date but no time, 12 A.M. is used. Any leading, inner, or trailing white space character in s is ignored.
Parameter s must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic.
Parameter provider supplies culture-specific date and time formatting information. For example, the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. If provider is null, the current culture is used.
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);
public static DateTime ParseExact( |
s
format
provider
Exception Type | Condition |
---|---|
ArgumentNullException | s or format is null. |
FormatException | s or format is an empty string -or- s does not contain a date and time that corresponds to the pattern specified in format. |
If s contains only a time, and no date, then the current date is used. If s contains only a date and no time, midnight (00:00:00) is used.
Parameter s cannot contain leading, inner, or trailing white space characters.
Parameter provider supplies culture-specific date and time formatting information. For example, the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. If provider is null, the current culture is used.
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);
public static DateTime ParseExact( |
s
format
provider
style
Exception Type | Condition |
---|---|
ArgumentNullException | s or format is null. |
FormatException | s or format is an empty string -or- s does not contain a date and time that corresponds to the pattern specified in format. |
If s contains a time but no date, the style parameter determines whether the current date or a default date is used. If s contains a date but no time, 12 A.M. is used. The style parameter also determines whether leading, inner, or trailing white space characters are ignored.
Parameter provider supplies culture-specific date and time formatting information. For example, the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. If provider is null, the current culture is used.
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);
public static DateTime ParseExact( |
s
formats
provider
style
Exception Type | Condition |
---|---|
ArgumentNullException | s or formats is null. |
FormatException | s is an empty string -or- an element of formats is an empty string -or- s does not contain a date and time that corresponds to any element of formats. |
If s contains a time but no date, the style parameter determines whether the current date or a default date is used. If s contains a date but no time, 12 A.M. is used. The style parameter also determines whether leading, inner, or trailing white space characters are ignored.
Parameter provider supplies culture-specific date and time formatting information. For example, the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. If provider is null, the current culture is used.
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);
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The result is less than DateTime.MinValue or greater than DateTime.MaxValue. |
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;
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The result is less than DateTime.MinValue or greater than DateTime.MaxValue. |
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 ToFileTime(); |
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The value of this instance cannot be converted to a system file time. |
An exception is thrown if you attempt to convert a date earlier than January 1, 1601 12:00:00 A.M. coordinated universal time (UTC).
static void Main(string[] args) { System.Console.WriteLine("Enter the file path:"); string filePath = System.Console.ReadLine(); if (System.IO.File.Exists(filePath)) { System.DateTime fileCreationDateTime = System.IO.File.GetCreationTime(filePath); long fileCreationFileTime = fileCreationDateTime.ToFileTime(); System.Console.WriteLine("{0} in file time is {1}.", fileCreationDateTime, fileCreationFileTime); } else { System.Console.WriteLine("{0} is an invalid file", filePath); } }
public DateTime ToLocalTime(); |
-or-
DateTime.MaxValue if the current UTC time is too large to be represented as a DateTime.
-or-
DateTime.MinValue if the current UTC time is too small to be represented as a DateTime.
This method always uses the local time zone when making calculations.
System.Console.WriteLine("Enter a date and time."); string strDateTime = System.Console.ReadLine(); System.DateTime localDateTime; try { localDateTime = System.DateTime.Parse(strDateTime); } catch (System.FormatException) { System.Console.WriteLine("Invalid format."); return; } System.DateTime univDateTime = localDateTime.ToUniversalTime(); System.Console.WriteLine("{0} local time is {1} universal time.", localDateTime, univDateTime); System.Console.WriteLine("Enter a date and time in universal time."); strDateTime = System.Console.ReadLine(); try { univDateTime = System.DateTime.Parse(strDateTime); } catch (System.FormatException) { System.Console.WriteLine("Invalid format."); return; } localDateTime = univDateTime.ToLocalTime(); System.Console.WriteLine("{0} universal time is {1} local time.", univDateTime, localDateTime);
public string ToLongDateString(); |
For more information about format characters, format patterns, and the output they produce, see the the conceptual topic at MSDN: formattingoverview topic. For more information about changing the format pattern associated with a format character see the DateTimeFormatInfo class.
This method uses formatting information derived from the current culture. For more information about the current culture, see the CultureInfo.CurrentCulture class. You can use the CultureInfo.DateTimeFormat property to obtain the DateTimeFormatInfo for the current culture.
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();
public string ToLongTimeString(); |
For more information about format characters, format patterns, and the output they produce, see the the conceptual topic at MSDN: formattingoverview topic. For more information about changing the format pattern associated with a format character see the DateTimeFormatInfo class.
This method uses formatting information derived from the current culture. For more information about the current culture, see the CultureInfo.CurrentCulture class. You can use the CultureInfo.DateTimeFormat property to obtain the DateTimeFormatInfo for the current culture.
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();
public double ToOADate(); |
Exception Type | Condition |
---|---|
OverflowException | The value of this instance cannot be represented as an OLE Automation Date. |
Only DateTime objects with a tick value that is greater than or equal to positive or negative 31241376000000000 can be represented as an OLE Automation Date. An uninitialized DateTime, that is, an instance with a tick value of 0, is converted to the equivalent uninitialized OLE Automation Date, that is, a date with a value of 0.0 which represents midnight, 30 December 1899.
public string ToShortDateString(); |
For more information about format characters, format patterns, and the output they produce, see the the conceptual topic at MSDN: formattingoverview topic. For more information about changing the format pattern associated with a format character see the DateTimeFormatInfo class.
This method uses formatting information derived from the current culture. For more information about the current culture, see the CultureInfo.CurrentCulture class. You can use the CultureInfo.DateTimeFormat property to obtain the DateTimeFormatInfo for the current culture.
public string ToShortTimeString(); |
For more information about format characters, format patterns, and the output they produce, see the the conceptual topic at MSDN: formattingoverview topic. For more information about changing the format pattern associated with a format character see the DateTimeFormatInfo class.
This method uses formatting information derived from the current culture. For more information about the current culture, see the CultureInfo.CurrentCulture class. You can use the CultureInfo.DateTimeFormat property to obtain the DateTimeFormatInfo for the current culture.
public override string ToString(); |
This method uses formatting information derived from the current culture. For more information, see CultureInfo.CurrentCulture.
using System; using System.Globalization; public class MainClass { public static void Main(string[] args) { DateTime dt = DateTime.Now; String[] format = { "d", "D", "f", "F", "g", "G", "m", "r", "s", "t", "T", "u", "U", "y", "dddd, MMMM dd yyyy", "ddd, MMM d \"'\"yy", "dddd, MMMM dd", "M/yy", "dd-MM-yy", }; String date; for (int i = 0; i < format.Length; i++) { date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo); Console.WriteLine(String.Concat(format[i], " :" , date)); } /** Output. * * d :08/17/2000 * D :Thursday, August 17, 2000 * f :Thursday, August 17, 2000 16:32 * F :Thursday, August 17, 2000 16:32:32 * g :08/17/2000 16:32 * G :08/17/2000 16:32:32 * m :August 17 * r :Thu, 17 Aug 2000 23:32:32 GMT * s :2000-08-17T16:32:32 * t :16:32 * T :16:32:32 * u :2000-08-17 23:32:32Z * U :Thursday, August 17, 2000 23:32:32 * y :August, 2000 * dddd, MMMM dd yyyy :Thursday, August 17 2000 * ddd, MMM d "'"yy :Thu, Aug 17 '00 * dddd, MMMM dd :Thursday, August 17 * M/yy :8/00 * dd-MM-yy :17-08-00 */ } }
public string ToString( |
provider
The provider parameter is typically an instance of DateTimeFormatInfo. If provider is null, the DateTimeFormatInfo associated with the current culture is used. For more information, see CultureInfo.CurrentCulture.
using System; using System.Globalization; public class MainClass { public static void Main(string[] args) { DateTime dt = DateTime.Now; String[] format = { "d", "D", "f", "F", "g", "G", "m", "r", "s", "t", "T", "u", "U", "y", "dddd, MMMM dd yyyy", "ddd, MMM d \"'\"yy", "dddd, MMMM dd", "M/yy", "dd-MM-yy", }; String date; for (int i = 0; i < format.Length; i++) { date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo); Console.WriteLine(String.Concat(format[i], " :" , date)); } /** Output. * * d :08/17/2000 * D :Thursday, August 17, 2000 * f :Thursday, August 17, 2000 16:32 * F :Thursday, August 17, 2000 16:32:32 * g :08/17/2000 16:32 * G :08/17/2000 16:32:32 * m :August 17 * r :Thu, 17 Aug 2000 23:32:32 GMT * s :2000-08-17T16:32:32 * t :16:32 * T :16:32:32 * u :2000-08-17 23:32:32Z * U :Thursday, August 17, 2000 23:32:32 * y :August, 2000 * dddd, MMMM dd yyyy :Thursday, August 17 2000 * ddd, MMM d "'"yy :Thu, Aug 17 '00 * dddd, MMMM dd :Thursday, August 17 * M/yy :8/00 * dd-MM-yy :17-08-00 */ } }
format
Exception Type | Condition |
---|---|
FormatException | The length of format is 1, and it is not one of the format specifier characters defined for DateTimeFormatInfo. -or- format does not contain a valid custom format pattern. |
If format is null or an empty string, the general format specifier, 'G', is used.
This method uses formatting information derived from the current culture. For more information, see CultureInfo.CurrentCulture.
using System; using System.Globalization; public class MainClass { public static void Main(string[] args) { DateTime dt = DateTime.Now; String[] format = { "d", "D", "f", "F", "g", "G", "m", "r", "s", "t", "T", "u", "U", "y", "dddd, MMMM dd yyyy", "ddd, MMM d \"'\"yy", "dddd, MMMM dd", "M/yy", "dd-MM-yy", }; String date; for (int i = 0; i < format.Length; i++) { date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo); Console.WriteLine(String.Concat(format[i], " :" , date)); } /** Output. * * d :08/17/2000 * D :Thursday, August 17, 2000 * f :Thursday, August 17, 2000 16:32 * F :Thursday, August 17, 2000 16:32:32 * g :08/17/2000 16:32 * G :08/17/2000 16:32:32 * m :August 17 * r :Thu, 17 Aug 2000 23:32:32 GMT * s :2000-08-17T16:32:32 * t :16:32 * T :16:32:32 * u :2000-08-17 23:32:32Z * U :Thursday, August 17, 2000 23:32:32 * y :August, 2000 * dddd, MMMM dd yyyy :Thursday, August 17 2000 * ddd, MMM d "'"yy :Thu, Aug 17 '00 * dddd, MMMM dd :Thursday, August 17 * M/yy :8/00 * dd-MM-yy :17-08-00 */ } }
public string ToString( |
format
provider
Exception Type | Condition |
---|---|
FormatException | The length of format is 1, and it is not one of the format specifier characters defined for DateTimeFormatInfo. -or- format does not contain a valid custom format pattern. |
If format is null or an empty string (""), the standard format specifier, 'G', is used.
The provider parameter is typically an instance of DateTimeFormatInfo. If provider is null, the DateTimeFormatInfo associated with the current culture is used. For more information, see CultureInfo.CurrentCulture.
using System; using System.Globalization; public class MainClass { public static void Main(string[] args) { DateTime dt = DateTime.Now; String[] format = { "d", "D", "f", "F", "g", "G", "m", "r", "s", "t", "T", "u", "U", "y", "dddd, MMMM dd yyyy", "ddd, MMM d \"'\"yy", "dddd, MMMM dd", "M/yy", "dd-MM-yy", }; String date; for (int i = 0; i < format.Length; i++) { date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo); Console.WriteLine(String.Concat(format[i], " :" , date)); } /** Output. * * d :08/17/2000 * D :Thursday, August 17, 2000 * f :Thursday, August 17, 2000 16:32 * F :Thursday, August 17, 2000 16:32:32 * g :08/17/2000 16:32 * G :08/17/2000 16:32:32 * m :August 17 * r :Thu, 17 Aug 2000 23:32:32 GMT * s :2000-08-17T16:32:32 * t :16:32 * T :16:32:32 * u :2000-08-17 23:32:32Z * U :Thursday, August 17, 2000 23:32:32 * y :August, 2000 * dddd, MMMM dd yyyy :Thursday, August 17 2000 * ddd, MMM d "'"yy :Thu, Aug 17 '00 * dddd, MMMM dd :Thursday, August 17 * M/yy :8/00 * dd-MM-yy :17-08-00 */ } }
public DateTime ToUniversalTime(); |
-or-
DateTime.MaxValue if the current local time is too large to be represented as a DateTime.
-or-
DateTime.MinValue if the current local time is too small to be represented as a DateTime.
This method assumes that the current DateTime holds the local time value, and not a UTC time. Therefore, each time it is run, the current method performs the necessary modifications on the DateTime to derive the UTC time, whether the current DateTime holds the local time or not.
This method always uses the local time zone when making calculations.
System.Console.WriteLine("Enter a date and time."); string strDateTime = System.Console.ReadLine(); System.DateTime localDateTime; try { localDateTime = System.DateTime.Parse(strDateTime); } catch (System.FormatException) { System.Console.WriteLine("Invalid format."); return; } System.DateTime univDateTime = localDateTime.ToUniversalTime(); System.Console.WriteLine("{0} local time is {1} universal time.", localDateTime, univDateTime); System.Console.WriteLine("Enter a date and time in universal time."); strDateTime = System.Console.ReadLine(); try { univDateTime = System.DateTime.Parse(strDateTime); } catch (System.FormatException) { System.Console.WriteLine("Invalid format."); return; } localDateTime = univDateTime.ToLocalTime(); System.Console.WriteLine("{0} universal time is {1} local time.", univDateTime, localDateTime);