Home » DateTime in C#

DateTime in C#

by Online Tutorials Library

DateTime in C#

We used the DateTime when there is a need to work with the dates and times in C#.

We can format the date and time in different formats by the properties and methods of the DateTime./p>

The value of the DateTime is between the 12:00:00 midnight, January 1 0001 and 11:59:59 PM, December 31, 9999 A.D.

Here we will explain how to create the DateTime in C#.

We have different ways to create the DateTime object. A DateTime object has Time, Culture, Date, Localization, Milliseconds.

Here we have a code which shows the various constructor uses by the DateTime structure to create the DateTime objects.

Properties of DateTime in C#

The DateTime has the Date and Time property. From DateTime, we can find the date and time. DateTime contains other properties as well, like Hour, Minute, Second, Millisecond, Year, Month, and Day.

The other properties of DateTime are:

  1. We can get the name of the day from the week with the help of the DayOfWeek property.
  2. To get the day of the year, we will use DayOfYear property.
  3. To get time in a DateTime, we use TimeOfDay property.
  4. Today property will return the object of the DateTime, which is having today’s value. The value of the time is 12:00:00
  5. The Now property will return the DateTime object, which is having the current date and time.
  6. The Utc property of DateTime will return the Coordinated Universal Time (UTC).
  7. The one tick represents the One hundred nanoseconds in DateTime. Ticks property of the DateTime returns the number of ticks in a DateTime.
  8. The Kind property returns value where the representation of time is done by the instance, which is based on the local time, Coordinated Universal Time (UTC). It also shows the unspecified default value.

Here we are taking an example of using the properties of the DateTime in the C# Code.

Example:

Output:

DateTime in C#

Addition and Subtraction of the DateTime in C#

The DateTime structure provides the methods to add and subtract the date and time to and from the DateTime object. We can add and subtract the date in the DateTime structure to and from the DateTime object. For the Addition and Subtraction in the DateTime, we use the TimeSpan structure.

For Addition and Subtraction, we can use the Add and Subtract method from the DateTime object. Firstly, we create the TimeSpan with the values of the date and time where we use the Add and Subtract methods.

Here we are creating a code that will add 3 and subtract the 30 days from today and display the day on the console.

DateTime structure contains the methods to add years, days, hours, minutes, seconds.

To add the different components to the DateTime object Add method is used.

The DateTime does not contain the subtract method. To subtract the component of the DateTime, we will use only the subtract method. For example: if we need to subtract the 12 days from the DateTime, we can create another object of the DateTime or TimeSpan object with 12 days. Now we will subtract this object from the DateTime. At the alternate of this, we can also use minus operator to subtract the DateTime or TimeSpan from the DateTime.

Now we will create a code through which we can create the object of the DateTime and subtract another DateTime and Object of TimeSpan. In code, we will show the subtraction of only the hours, days, or other components from the DateTime.

Searching of the Days in the Month

To find the number of days in the month, we used the static DaysInMonth method. This searching method [] takes the parameter in numbers from 1 to 12.

Here we will write a code through which we will find out the number of days in a particular month.

Here we will find out the number of days in Feb in 2020. The output will be 28 days.

With the same technique, we can find out the total number of days in a year. For that, we will use the method DaysInYear.

Comparison of two DateTime in C#

The comparer static method is used to compare the object of the two datetime. If the objects of both DateTime is the same, then the result will be 0. If the first DateTime is earlier, then the result will be 0 else the first DateTime would be later.

Now we will show the comparison of the two datetime objects in C#.

Output:

DateTime in C#

CompareTo Method

CompareTo method is used to compare the two dates. We will assign the DateTime or object in this method.

To compare the two DateTime object, we used the CompareTo method. Below we have a C# code to compare the DateTime object.

Output:

DateTime in C#

Formatting of the DateTime in C#

In C#, we can format the DateTime to any type of string format as we want.

For the formatting of the DateTime, we used the GetDateTimeFormats method, which returns all the possible DateTime formats for the current culture of the computer.

Here we have a C# code that returns the array of the strings of all the possible standard formats.

Output:

DateTime in C#
DateTime in C#

We can overload the GetDateTimeFormats method, which takes the format specifier as a parameter and converts the DateTime to that format. To get the desired format, we need to understand the format of the DateTime specifiers.

We will show it with the code with the pattern in a table.

Code Pattern
“d” Short date
“D” Long date
“f” Full date time. Short time.
“F” Full date time. Long Time.
“g” Generate date time. Long Time.
“G” General date time. Long Time.
“M”,”m.” Month/day
“O”,”o” Round trip date/time.
“R”,”r” RFC1123
“s” Sortable date time.
“t” Sort Time
“T” Long Time
“u” Universal sortable date time.
“U” Universal full date-time.
“Y”,”y” Year, Month

We will specify the format of the DateTime in the below C# Code.

Output:

DateTime in C#
DateTime in C#

We can also do the formatting of the DateTime by passing the format specifier in the ToString() method of DateTime. Now we will write the C# code for the formatting of the DateTime using the ToString() method.

Now we will write a C# code for the DateTime format specifiers within the ToString() method.

DateTime in C#

Get the Leap Year and Daylight-Saving Time

Through the C# Code, we will get the Leap Year and Daylight-Saving Time.

Output:

DateTime in C#

Conversion of string to the DateTime

To convert the string to a DateTime object, we used the Parse method. In the Parse method, the passing string must have the correct format of the DateTime. For the conversion of the DateTime to the String, the ToString() method is used.  

Output:

DateTime in C#

Conversion of DateTime in C#

The structure of the DateTime is full of self-explanatory conversion, which converts the DateTime to the specific type. The methods are ToFileTime, ToLocalTime, ToLongDateString, ToBinary ,ToLongTimeString, ToOADate, ToShortDateString, ToShortTimeString, ToString, and ToUniversalTime.

Here we will take an example of C# to convert the DateTime to the specific type.

Output:

DateTime in C#


Next TopicType Casting in C#

You may also like