DateTime in C#

DateTime in C#

Date and Time in C# are handled by the DateTime class in C#, which provides properties and methods to format dates in different datetime formats.

The following table describes various C# DateTime formats and their results. Here we see  all the patterns of the C# DateTime, format, and results

FormatResult
DateTime.Now.ToString(“MM/dd/yyyy”)05/29/2015
DateTime.Now.ToString(“dddd, dd MMMM yyyy”)Friday, 29 May 2015
DateTime.Now.ToString(“dddd, dd MMMM yyyy”)Friday, 29 May 2015 05:50
DateTime.Now.ToString(“dddd, dd MMMM yyyy”)Friday, 29 May 2015 05:50 AM
DateTime.Now.ToString(“dddd, dd MMMM yyyy”)Friday, 29 May 2015 5:50
DateTime.Now.ToString(“dddd, dd MMMM yyyy”)Friday, 29 May 2015 5:50 AM
DateTime.Now.ToString(“dddd, dd MMMM yyyy HH:mm:ss”)Friday, 29 May 2015 05:50:06
DateTime.Now.ToString(“MM/dd/yyyy HH:mm”)05/29/2015 05:50
DateTime.Now.ToString(“MM/dd/yyyy hh:mm tt”)05/29/2015 05:50 AM
DateTime.Now.ToString(“MM/dd/yyyy H:mm”)05/29/2015 5:50
DateTime.Now.ToString(“MM/dd/yyyy h:mm tt”)05/29/2015 5:50 AM
DateTime.Now.ToString(“MM/dd/yyyy HH:mm:ss”)05/29/2015 05:50:06
DateTime.Now.ToString(“MMMM dd”)May 29
DateTime.Now.ToString(“yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK”)2015-05-16T05:50:06.7199222-04:00
DateTime.Now.ToString(“ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’”)Fri, 16 May 2015 05:50:06 GMT
DateTime.Now.ToString(“yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss”)2015-05-16T05:50:06
DateTime.Now.ToString(“HH:mm”)05:50
DateTime.Now.ToString(“hh:mm tt”)05:50 AM
DateTime.Now.ToString(“H:mm”)5:50
DateTime.Now.ToString(“h:mm tt”)5:50 AM
DateTime.Now.ToString(“HH:mm:ss”)05:50:06
DateTime.Now.ToString(“yyyy MMMM”)2015 May

Basic Stuctures:

// Declaration without initialization
DateTime dateTimeVariable;

// Declaration with initialization to a specific date and time
DateTime dateTimeVariable = new DateTime(year, month, day, hour, minute, second);

// Declaration with initialization to the current date and time
DateTime currentDateTime = DateTime.Now;

Example:

using System;
class CurrentDateTimeExample
{
    public static void Main()
    {
        DateTime Default = DateTime.Now; // Define date time format default from system
        string Custom = DateTime.Now.ToString(“MM/dd/yyyy hh:mm tt”); // Define date time by custom format
        Console.WriteLine(“Current Date and Time: ” + Default);
        Console.WriteLine(“Current Date and Time: ” + Custom);
    }
}

d: Represents the day of the month as a number from 1 through 31.

dd: Represents the day of the month as a number from 01 through 31.

ddd: Represents the abbreviated name of the day (Mon, Tues, Wed, etc).

dddd: Represents the full name of the day (Monday, Tuesday, etc).

h: 12-hour clock hour (e.g. 4).

hh: 12-hour clock, with a leading 0 (e.g. 06)

H: 24-hour clock hour (e.g. 15)

HH: 24-hour clock hour, with a leading 0 (e.g. 22)

m: Minutes

mm: Minutes with a leading zero

M: Month number(eg.3)

MM: Month number with leading zero(eg.04)

MMM: Abbreviated Month Name (e.g. Dec)

MMMM: Full month name (e.g. December)

s: Seconds

ss: Seconds with leading zero

t: Abbreviated AM / PM (e.g. A or P)

tt: AM / PM (e.g. AM or PM

y: Year, no leading zero (e.g. 2015 would be 15)

yy: Year, leading zero (e.g. 2015 would be 015)

yyy: Year, (e.g. 2015)

yyyy: Year, (e.g. 2015)

K: Represents the time zone information of a date and time value (e.g. +05:00)

z: With DateTime values represent the signed offset of the local operating system’s time zone from
Coordinated Universal Time (UTC), measured in hours. (e.g. +6)

zz: As z, but with leading zero (e.g. +06)

zzz: With DateTime values represents the signed offset of the local operating system’s time zone from UTC, measured in hours and minutes. (e.g. +06:00)

f: Represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value.

ff: Represents the two most significant digits of the second’s fraction in date and time

fff: Represents the three most significant digits of the second’s fraction; that is, it represents the milliseconds in a date and time value.

ffff: Represents the four most significant digits of the second’s fraction; that is, it represents the ten-thousandths of a second in a date and time value. While it is possible to display the ten thousandths of a second component of a time value, that value may not be meaningful.

fffff: Represents the five most significant digits of the second’s fraction; that is, it represents the hundred-thousandths of a second in a date and time value.

ffffff: Represents the six most significant digits of the second’s fraction; that is, it represents the millionths of a second in a date and time value.

fffffff: Represents the seven most significant digits of the second’s fraction; that is, it represents the ten-millionths of a second in a date and time value.

Course Video

Course Video In English
Task:

1. Current Date and Time:
Print the current date and time to the console using DateTime.Now.

2. Date Formatting:
Print the current date in the format “yyyy-MM-dd” (e.g., “2024-07-06”).

3. Date Comparison:
Create two DateTime objects representing different dates.
Compare these dates to determine which one is earlier or if they are equal.

4. Date Manipulation:
Create a DateTime object representing today’s date.
Add 5 days to this date and print the result.

5. Age Calculation:
Ask the user to enter their birthdate.
Calculate their age based on the current date and print it.

Frequently Asked Questions

Still have a question?

Let's talk

The DateTime class in C# represents date and time values. It allows data to be stored, manipulated, formatted, and compared between date and time. It’s one of the most essential classes in C# for working with time-based operations.

DateTime.Now returns the current date and time in your local time zone, while DateTime.UtcNow returns the current time in Coordinated Universal Time (UTC), which is not affected by time zones.

Yes, while DateTime itself does not store time zone information, you can use the DateTimeOffset class, which includes both the date and time along with the time zone offset, making it ideal for working with different time zones.

DateTime.MinValue represents the smallest possible date (January 1, 0001), and DateTime.MaxValue represents the largest possible date (December 31, 9999).

DateTimeKind specifies whether a DateTime object represents local time, UTC, or an unspecified time zone. This helps in ensuring consistency when working with different time zones.

No, DateTime objects are immutable. You cannot change the value of an existing DateTime instance. However, you can create a new DateTime by using methods like AddDays(), AddMonths(), or AddYears().

The DateTime structure in C# represents dates and times. It provides methods and properties to work with date and time values, such as getting the current date, adding days, and formatting dates.

Here are some commonly used methods:

  • AddDays(int days): Adds a specified number of days to the date.
  • AddMonths(int months): Adds months to the date.
  • ToString(string format): Converts the date to a string using a format.
  • Subtract(DateTime value): Finds the difference between two dates.

You can learn the DateTime class in Hindi with free online video tutorials for beginners at Iqra Academy, complete with examples and exercises.

  • DateTime.Now: Returns the current local date and time.
  • DateTime.UtcNow: Returns the current date and time in Coordinated Universal Time (UTC).

Yes, Iqra Academy offers tutorials in Hindi that explain DateTime methods, formatting, and practical examples for beginners.