System.DateTime Structure
The System.DateTime
structure represents an instant in time, typically expressed as a date and time of day.
Syntax
public struct DateTime
{
// Fields
private long _dateData;
// Constructors
public DateTime(long ticks);
public DateTime(int year, int month, int day);
public DateTime(int year, int month, int day,
int hour, int minute, int second);
public DateTime(int year, int month, int day,
int hour, int minute, int second,
DateTimeKind kind);
// ... (other members omitted for brevity)
}
Constructors
Signature | Description |
---|---|
DateTime(long ticks) | Initializes a new instance using the specified number of ticks. |
DateTime(int year, int month, int day) | Initializes a new instance to the specified year, month, and day. The time is set to 00:00:00. |
DateTime(int year, int month, int day, int hour, int minute, int second) | Initializes a new instance to the specified date and time. |
DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind) | Initializes a new instance to the specified date, time, and kind. |
Properties
Name | Type | Description |
---|---|---|
Now | DateTime | Gets the current date and time on this computer, expressed as the local time. |
UtcNow | DateTime | Gets the current date and time expressed as the Coordinated Universal Time (UTC). |
Date | DateTime | Gets the date component of this instance with the time component set to midnight. |
Day | int | Gets the day of the month represented by this instance. |
Month | int | Gets the month component of the date represented by this instance. |
Year | int | Gets the year component of the date represented by this instance. |
Hour | int | Gets the hour component of the time represented by this instance. |
Minute | int | Gets the minute component of the time represented by this instance. |
Second | int | Gets the second component of the time represented by this instance. |
Kind | DateTimeKind | Gets a value indicating whether the time represented is local time, UTC, or neither. |
Methods
Signature | Description |
---|---|
Add(TimeSpan value) | Returns a new DateTime that adds the specified time interval to the value of this instance. |
AddDays(double value) | Returns a new DateTime that adds the specified number of days to the value of this instance. |
AddHours(double value) | Returns a new DateTime that adds the specified number of hours to the value of this instance. |
Subtract(TimeSpan value) | Returns a new DateTime that subtracts the specified time interval from the value of this instance. |
ToString(string format) | Converts the value of the current DateTime object to its equivalent string representation using the specified format. |
ToUniversalTime() | Converts the value of the current DateTime object to Coordinated Universal Time (UTC). |
ToLocalTime() | Converts the value of the current DateTime object to local time. |
Parse(string s) | Converts the specified string representation of a date and time to its DateTime equivalent. |
TryParse(string s, out DateTime result) | Tries to convert a string representation of a date and time to its DateTime equivalent and returns a boolean indicating success. |
Fields
Name | Type | Description |
---|---|---|
MinValue | DateTime | The smallest possible value of DateTime. Represents 00:00:00.0000000 UTC, January 1, 0001. |
MaxValue | DateTime | The largest possible value of DateTime. Represents 23:59:59.9999999 UTC, December 31, 9999. |
Operators
Operator | Description |
---|---|
+ | Adds a TimeSpan to a DateTime, producing a new DateTime. |
- | Subtracts a TimeSpan from a DateTime, or returns the TimeSpan between two DateTimes. |
== | Determines whether two DateTime values are equal. |
!= | Determines whether two DateTime values are not equal. |
< | Determines whether one DateTime is earlier than another. |
> | Determines whether one DateTime is later than another. |
Examples
Display the current date and time in various formats:
using System;
class Program
{
static void Main()
{
DateTime now = DateTime.Now;
Console.WriteLine($"Default: {now}");
Console.WriteLine($"ISO 8601: {now:yyyy-MM-ddTHH:mm:ss.fffK}");
Console.WriteLine($"Long Date: {now:D}");
Console.WriteLine($"Short Time: {now:t}");
}
}
Calculate the number of days between two dates:
using System;
class AgeCalculator
{
static void Main()
{
DateTime birthday = new DateTime(1990, 5, 15);
DateTime today = DateTime.Today;
TimeSpan age = today - birthday;
Console.WriteLine($""You've lived {age.Days} days."");
}
}