Represents an instant in time, typically expressed as a date and time of day.
Constructors
DateTime ( )
Initializes a new
DateTime instance to the current date and time.
Syntax:DateTime ( )
Example:
dt = new DateTime( );
DateTime ( DateTime value )
Initializes a new
DateTime instance to the specified value.
Syntax:Parameters:Example:
dt1 = new DateTime();
dt2 = new DateTime( dt1 );
// is equivalent to
dt2 = dt1;
Initializes a new
DateTime instance to the specified year, month, and day.
Syntax:Parameters:year: The year.
month: The month.
day: The numeric day of the month.
Example:
dt = new DateTime( 2008, 21, 1); // results in a date/time of January 21st, 2008 12:00:00 Midnight
Initializes a new
DateTime instance to the specified year, month, day, hour, minute, and second.
Syntax:Parameters:year: The year.
month: The month.
day: The numeric day of the month.
hour: The hour.
minute: The minute.
second: The seconds.
Example:
dt = new DateTime( 2008, 21, 1, 14, 58, 2); // results in a date/time of January 21st, 2008 2:58:02 PM
Static Properties
| Property Name | Type | Read/Write | Description |
| Now | DateTime | Read Only | Gets a DateTime object that is set to the current date and time, expressed in local time. |
| Today | DateTime | Read Only | Gets a DateTime object that is set to the current date at midnight. |
Instance Properties
| Property Name | Type | Read/Write | Description |
| Day | Number | Read Only | Gets the day component of the date. |
| Month | Number | Read Only | Gets the month component of the date. |
| Year | Number | Read Only | Gets the year component of the date. |
| Hour | Number | Read Only | Gets the hour component of the date. |
| Minute | Number | Read Only | Gets the minute component of the date. |
| Second | Number | Read Only | Gets the seconds component of the date. |
| Millisecond | Number | Read Only | Gets the milliseconds component of the date. |
| DayOfWeek | Number | Read Only | Gets the day of the week. 0=Sunday, 1=Monday, etc. |
| DayOfYear | Number | Read Only | Gets the day of the year. |
| IsWeekday | Boolean | Read Only | Gets a boolean indicating if the date is a weekday (Monday through Friday). |
| IsWeekend | Boolean | Read Only | Gets a boolean indicating if the date is a weekend (Saturday or Sunday). |
Instance Methods
AddDays
Adds the specified number of days to the value of this instance.
Syntax:Parameters:days : A number of whole and fractional days. The value parameter can be negative or positive.
Returns:A
DateTime whose value is the sum of the date and time represented by this instance and the number of days represented by value.
Example:
dt.AddDays( 1 );
AddHours
Adds the specified number of hours to the value of this instance.
Syntax:Parameters:hours : A number of whole and fractional hours. The value parameter can be negative or positive.
Returns:A
DateTime whose value is the sum of the date and time represented by this instance and the number of hours represented by value.
Example:
dt.AddHours( 2 );
AddMilliseconds
Adds the specified number of milliseconds to the value of this instance.
Syntax:Parameters:milliseconds : A number of whole and fractional milliseconds. The value parameter can be negative or positive. Note that this value is rounded to the nearest integer.
Returns:A
DateTime whose value is the sum of the date and time represented by this instance and the number of milliseconds represented by value.
Example:
dt.AddMilliseconds( 500 );
AddMinutes
Adds the specified number of minutes to the value of this instance.
Syntax:Parameters:minutes : A number of whole and fractional minutes. The value parameter can be negative or positive.
Returns:A
DateTime whose value is the sum of the date and time represented by this instance and the number of minutes represented by value.
Example:
dt.AddMinutes( 15 );
AddMonths
Adds the specified number of months to the value of this instance.
Syntax:Parameters:months : A number of months. The months parameter can be negative or positive.
Returns:A
DateTime whose value is the sum of the date and time represented by this instance and months.
Example:
dt.AddMonths( 3 );
AddSeconds
Adds the specified number of seconds to the value of this instance.
Syntax:Parameters:seconds : A number of whole and fractional seconds. The value parameter can be negative or positive.
Returns:A
DateTime whose value is the sum of the date and time represented by this instance and the number of seconds represented by value.
Example:
dt.AddSeconds( 3 );
AddYears
Adds the specified number of years to the value of this instance.
Syntax:Parameters:years : A number of years. The value parameter can be negative or positive.
Returns:A
DateTime whose value is the sum of the date and time represented by this instance and the number of years represented by value.
Example:
dt.AddYears( 4 );
ToString ( )
Converts the value of this instance to its equivalent string representation using the computer's current locale format.
Syntax:Returns:A
String representation of value of this instance.
Examples:
strToday = new DateTime().ToString( );
ToString ( String )
Converts the value of this instance to its equivalent string representation using the specified format.
Syntax:Parameters:format : A format string.
- Example Format Strings:
- "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
Returns:A
String representation of value of this instance as specified by format.
Examples:
strToday = new DateTime().ToString( "dddd, MMMM dd yyyy" ); // results in "Thursday, January 17 2008"
strTomorrow = new DateTime().AddDays(1).ToString("MM/dd/yyyy");