Namespace: System

Represents an instance in time, typically expressed as a date and time of day. The value is expressed as the number of ticks that have passed since 12:00:00 midnight, January 1, 0001 A.D. (according to the Gregorian calendar), on the local time.

The DateTime structure is a value type that represents dates and times. It is used to represent a specific point in time, such as the current date and time, or a date and time from a file or database.

The DateTime structure supports the Gregorian calendar and can represent dates ranging from January 1, 0001 A.D. (midnight) to December 31, 9999 A.D. (11:59:59 P.M.).

Constructors

DateTime(int year, int month, int day)

public DateTime(int year, int month, int day);

Initializes a new instance of the DateTime structure to the specified year, month, and day.

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);

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second.

Properties

Year

public int Year { get; }

Gets the year component of the date represented by this instance.

Month

public int Month { get; }

Gets the month component of the date represented by this instance.

Day

public int Day { get; }

Gets the day component of the date represented by this instance.

Hour

public int Hour { get; }

Gets the hour component of the date represented by this instance.

Minute

public int Minute { get; }

Gets the minute component of the date represented by this instance.

Second

public int Second { get; }

Gets the second component of the date represented by this instance.

Now

public static DateTime Now { get; }

Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time.

Methods

AddDays(double value)

public DateTime AddDays(double value);

Returns a new DateTime object that adds the specified number of days to the value of this instance.

ToString()

public override string ToString();

Converts the value of this instance to its equivalent string representation.

Static Methods

Parse(string s)

public static DateTime Parse(string s);

Converts the string representation of a date and time to its DateTime equivalent.

TryParse(string s, out DateTime result)

public static bool 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.

Example

The following example demonstrates how to create a DateTime object and use some of its properties and methods:


using System;

public class Example
{
    public static void Main(string[] args)
    {
        // Get the current date and time
        DateTime now = DateTime.Now;
        Console.WriteLine($"Current date and time: {now}");

        // Create a specific date and time
        DateTime christmas = new DateTime(2023, 12, 25, 10, 30, 0);
        Console.WriteLine($"Christmas: {christmas}");

        // Add days to a date
        DateTime futureDate = now.AddDays(7);
        Console.WriteLine($"In 7 days: {futureDate}");

        // Parse a date string
        string dateString = "01/15/2024";
        DateTime parsedDate;
        if (DateTime.TryParse(dateString, out parsedDate))
        {
            Console.WriteLine($"Parsed date: {parsedDate}");
        }
        else
        {
            Console.WriteLine($"Failed to parse date: {dateString}");
        }
    }
}