Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
data types
datetime

How to use DateTime in C#

Educative Answers Team

DateTime

DateTime is a built-in class in C# used to store dates and parse them to and from strings.

Usage

There are various ways to format time in C#:

using System;
class HelloWorld
{
    static void Main()
    {
        // Storing date as String
        string dateString = "15/07/2020 06:30:45 PM";
        
        // Directly initializing date as DateTime variable
        DateTime date1 = new DateTime(2020, 7, 15, 18, 30, 45);
        // Parsing string to DateTime
        DateTime date2 = DateTime.ParseExact(dateString, "dd/MM/yyyy hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);
        
        // Both dates output 07 15 20 18:30:45
        Console.WriteLine(date1.ToString("MM dd yy HH:mm:ss"));
        Console.WriteLine(date2.ToString("MM dd yy HH:mm:ss"));

        // Outputs Wednesday July, 2020 06:30:45 PM
        Console.WriteLine(date1.ToString("dddd MMMM, yyyy hh:mm:ss tt"));

        // Outputs 07/15/2020
        Console.WriteLine(date1.ToString("MM/dd/yyyy"));

        // Outputs 06:30:45 PM
        Console.WriteLine(date1.ToString("hh:mm:ss tt"));
    }
}

This example demonstrates how DateTime variables are initialized and then parsed to strings. Parsing to and from a string can be done by comparing the string to a user-defined format.

C# allows users to define their own format using strings. Different components of time are represented in the format as different strings. Some of these are:

  1. d represents the day without a leading 00. It ranges from 11 to 3131.
  2. dd represents the day with a leading 00. It ranges from 0101 to 3131.
  3. ddd represents an abbreviated name for a day like Mon or Tues.
  4. dddd represents the full name for a day like Monday or Tuesday.
  5. M is the month number without a leading zero. It ranges from 11 to 1212.
  6. MM is the month number with a leading zero. It ranges from 0101 to 1212.
  7. MMM is an abbreviated name of a month like Jan or Feb.
  8. MMMM is the full name of a month like January or February.
  9. y is the year with a minimum of one digit. For instance, 2001 would be 11.
  10. yy is the year with a minimum of two digits. For instance, 2001 would be 0101.
  11. yyy is the year with a minimum of three digits. For instance, 2001 would be 001001.
  12. yyyy is a four-digit representation of a year. For instance, 2001 would be 20012001.
  13. h is the hour in 12-hour notation without a leading zero. For example, 4 AM and 4 PM would both be 44.
  14. hh is the hour in 12-hour notation with a leading zero. For example, 4 AM and 4 PM would both be 0404.
  15. H is the hour in 24-hour notation without a leading zero. For example, 4 AM and 4 PM would be 44 and 1616, respectively.
  16. HH is the hour in 24-hour notation with a leading zero. For example, 4 AM and 4 PM would be 0404 and 1616, respectively.
  17. m is the minutes without a leading zero. It ranges from 00to 5959.
  18. mm is the minutes with a leading zero. It ranges from 0000 to 5959.
  19. s is the seconds without a leading zero. It ranges from 00 to 6060.
  20. ss is the seconds with a leading zero. It ranges from 0000 to 6060.
  21. t is a single-character abbreviation for AM and PM. AM will become A and PM will become P.
  22. tt is AM and PM.
  23. K is the time-zone. For example, +05:00+05:00.
  24. z represents a minimum single-digit offset from the Coordinated Universal Timezone in hours. For example, +5+5.
  25. zz represents a minimum two-digit offset from the Coordinated Universal Timezone in hours. For example, +05+05.
  26. zz represents an offset from the Coordinated Universal Timezone in hours and minutes. For example, +05:00+05:00.
  27. f represents the decimals in a seconds value. The number of decimal digits is equal to the number of f’s in the format – there can be up to seven f’s. For example, fff may show 224224 for a seconds value of 2:22459 and 000000 for 2:00000.
  28. F represents the non-zero decimals in a seconds value. The number of decimal digits is equal to the number of F’s in the format – there can be up to seven F’s. For example, FFF may show 224224 for a seconds value of 2:22459, but it would show nothing for 2:00000.

RELATED TAGS

c#
data types
datetime
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring