System.DateTime Structure
Represents an instance of time. The value is an ordered pair of a date and a time of day.
Summary
The System.DateTime
structure represents an instant in time, expressed as a date and time of day. It is a value type that can store values ranging from 12:00:00 AM January 1, 0001, to 11:59:59 PM December 31, 9999, in the Gregorian calendar. DateTime
values are represented internally as the number of 100-nanosecond intervals that have elapsed since 12:00:00 AM January 1, 0001, Coordinated Universal Time (UTC). This structure is fundamental for handling date and time operations in .NET applications.
Properties
Name | Description |
---|---|
Date |
Gets the date component of this instance that is expressed as the current date. |
Day |
Gets the day of the month represented by this instance. |
DayOfWeek |
Gets the day of the week represented by this instance. |
DayOfYear |
Gets the day of the year represented by this instance. |
Hour |
Gets the hour component of the date represented by this instance. |
Kind |
Gets a value indicating whether the DateTime value is stored as one of the following: local time, Coordinated Universal Time (UTC), or with a kind that is neither local time nor UTC. |
Millisecond |
Gets the millisecond component of the date represented by this instance. |
Minute |
Gets the minute component of the date represented by this instance. |
Month |
Gets the month component of the date represented by this instance. |
Now |
Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time. |
Second |
Gets the second component of the date represented by this instance. |
Ticks |
Gets the value of this instance expressed as the number of 100-nanosecond intervals that have elapsed since 12:00:00 AM January 1, 0001, UTC. |
TimeOfDay |
Gets a TimeSpan object that represents the time elapsed since midnight at the beginning of the current day. |
Today |
Gets a DateTime object that is set to the current date on this computer and whose time value is set to 12:00:00 AM (00:00:00). |
UtcNow |
Gets a DateTime object that is set to the current Coordinated Universal Time (UTC). |
Year |
Gets the year component of the date represented by this instance. |
Methods
Name | Description |
---|---|
Add(TimeSpan value) |
Adds the specified time interval to this instance. |
AddDays(double value) |
Adds the specified number of days to this instance. |
AddHours(double value) |
Adds the specified number of hours to this instance. |
AddMilliseconds(double value) |
Adds the specified number of milliseconds to this instance. |
AddMinutes(double value) |
Adds the specified number of minutes to this instance. |
AddMonths(int months) |
Adds the specified number of months to this instance. |
AddSeconds(double value) |
Adds the specified number of seconds to this instance. |
AddTicks(long value) |
Adds the specified number of ticks to this instance. |
AddYears(int value) |
Adds the specified number of years to this instance. |
CompareTo(DateTime value) |
Compares the current instance to another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in sort order as the other object. |
Equals(DateTime value) |
Returns a value that indicates whether this instance is equal to a specified DateTime value. |
Parse(string s) |
Converts the string representation of a date and time to its DateTime equivalent. |
ParseExact(string s, string format, IFormatProvider provider) |
Converts the string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. |
Subtract(DateTime value) |
Subtracts the specified DateTime value from this instance. |
ToString() |
Converts the value of this instance to its equivalent string representation. |
ToString(string format) |
Converts the value of this instance to its equivalent string representation using the specified format. |
ToUniversalTime() |
Converts the current DateTime value to Coordinated Universal Time (UTC). |
ToLocalTime() |
Converts the current DateTime value to local time. |
TryParse(string s, out DateTime result) |
Converts the string representation of a date and time to its DateTime equivalent. A return value indicates whether the conversion succeeded or failed. |
Constructors
Name | Description |
---|---|
DateTime(int year, int month, int day) |
Initializes a new instance of the DateTime structure to the specified number of years, months, and days. |
DateTime(int year, int month, int day, int hour, int minute, int second) |
Initializes a new instance of the DateTime structure to the specified number of years, months, days, hours, minutes, and seconds. |
DateTime(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 number of years, months, days, hours, minutes, seconds, and milliseconds. |
DateTime(long ticks) |
Initializes a new instance of the DateTime structure to the value indicated by the specified number of ticks. |
Usage Example
Here's a simple example demonstrating common DateTime
operations:
using System; public class DateTimeExample { public static void Main() { // Get the current date and time DateTime now = DateTime.Now; Console.WriteLine($"Current date and time: {now}"); // Get the current UTC date and time DateTime utcNow = DateTime.UtcNow; Console.WriteLine($"Current UTC date and time: {utcNow}"); // Create a specific date and time DateTime specificDate = new DateTime(2023, 10, 26, 14, 30, 0); Console.WriteLine($"Specific date and time: {specificDate}"); // Add days to a date DateTime futureDate = specificDate.AddDays(7); Console.WriteLine($"Date after 7 days: {futureDate}"); // Get the day of the week Console.WriteLine($"Day of the week for specificDate: {specificDate.DayOfWeek}"); // Format a DateTime object Console.WriteLine($"Formatted date: {specificDate.ToString("yyyy-MM-dd HH:mm:ss")}"); // Parse a string into a DateTime object string dateString = "2024-01-01 10:00:00"; if (DateTime.TryParse(dateString, out DateTime parsedDate)) { Console.WriteLine($"Parsed date: {parsedDate}"); } else { Console.WriteLine("Failed to parse date string."); } } }
Note: The exact formatting of
DateTime
objects can vary based on culture settings. Use format strings to ensure consistent output.