C# DateTime

Introduction

The DateTime is used to represent the instant time of the current system. It is usually expressed as the date and time of day. This allows us to find the current time, coordinated universal time and so on.

Creating a DateTime:

We can create DateTime in run-time only.Let's create a DateTime in Runtime,and then you use the DateTime struct.

Let's create a DateTime in Runtime,then you use the DateTime structure.

DateTime date = newDateTime (2021,2,17,11,12,10, 50);

Year=2021;
Month=2;
Day=17;
Hour=11;
Minute=12;
Second=10;
Millisecond=50;

Properties of DateTime:

We will learn about all the properties of the DateTime and use them.

  • Date:

    The Date properties return that value that is sent by us into the DateTime constructor.

    Console.WriteLine(date.Date);
    
  • Day:

    The Day properties return the day of the month that is represented by the DateTime.

    Console.WriteLine(date.Day);
    
    Ouput
    17
  • DayOfWeek:

    The DayOfWeek properties return the day of the week that is represented by the DateTime.

    Console.WriteLine(date.DayOfWeek);
    
    Ouput
    Wednesday
  • DayOfYear:

    TheDayOfYear property returns the day of the year that is represented by the DateTime.

    Console.WriteLine(date.DayOfYear);
    
    Ouput
    48
  • Hour:

    The hour property returns the hour component that is represented by the DateTime.

    Console.WriteLine(date.Hour);
    
    Ouput
    11
  • Millisecond:

    The Millisecond property returns the millisecond component that is represented by the DateTime.

    Console.WriteLine(date.Millisecond);
    
    Ouput
    50
  • Minute:

    The Minute property returns the minute component that is represented by the DateTime.

    Console.WriteLine(date.Minute);
    
    Ouput
    12
  • Month:

    The Month property returns the month component that is represented by the DateTime.

    Console.WriteLine(date.Month);
    
    Ouput
    2
  • Second:

    The Second property returns the second component that is represented by the DateTime.

    Console.WriteLine(date.Second);
    
    Ouput
    10
  • Ticks:

    The ticks property gets the number of ticks that represent the date and time of this instance.

    Console.WriteLine(date.Ticks);
    
    Ouput
    637491571300500000
  • Year:

    The Year gets the year that is represent by the DateTime.

    Console.WriteLine(date.Year);
    
    Ouput
    2021

Functions of DateTime:

We will learn about all the functions of the DateTime and use them.

  • Add() and Subtract():

    DateTime structure provides methods to add and subtract date and time to and from a DateTime object. The Timespan structure plays a major role in addition and subtraction.

    TimeSpants = newTimeSpan(11, 10, 10);
    Console.WriteLine("Adding " + date.Add(ts));
    Console.WriteLine("Substract " + date.Subtract(ts));
    
    Ouput
    Adding 17-02-2021 22:22:20
    Substract 17-02-2021 00:02:00
    
  • Compare between Two Dates:

    Datetime.Compare() function compare between two dates and returns an integer that indicates whether the first instance earlier than, the same as, or later than the second instance.

    DateTime date1 = newDateTime(2000, 12, 05);
    //returns today date
    DateTime date2 = DateTime.Today;
    int result = DateTime.Compare(date1, date2);
    if (result > 0)
    {
    Console.WriteLine("First date bigger than second date");
    }
    else
    {
    Console.WriteLine("Second date bigger than first date");
    }
    
    Ouput
    Second date bigger than first date
  • DateTime Format:

    GetDateTimeFormats() converts the value of this instance to all the string representations supported by the standard System.DateTime format specifiers.

    DateTime date1 = newDateTime(2000, 12, 05);
    string[] formats = date1.GetDateTimeFormats();
    foreach (string f in formats)
    {
    Console.WriteLine(f);
    }
    
    Ouput
    DateTime Format
  • IsLeapYear():

    IsLeapYear() methods returns an bool indication whether the specified year is a leap year or not.

    DateTime date1 = newDateTime(2000, 12, 05);
    if (DateTime.IsLeapYear(date1.Year))
    {
    Console.WriteLine(date1.Year + " is leap year");
    }
    else
    {
    Console.WriteLine(date1.Year + " is not leap year");
    }
    
    Ouput
    2000 is leap year
  • Convert DateTime:

    DateTime structure are many conversion methods are present. These methods are ToBinary, ToFileTime, ToLocalTime, ToLongDateString, ToLongTimeString, ToOADate, ToShortDateString, ToShortTimeString, ToString, and so on.

    DateTimedt = newDateTime(2000, 12, 05);
    Console.WriteLine("ToBinary() - "+dt.ToBinary());
    Console.WriteLine("ToFileTime() - " + dt.ToFileTime());
    Console.WriteLine("ToLocalTime() - " + dt.ToLocalTime());
    Console.WriteLine("ToFileTimeUtc() - "+dt.ToFileTimeUtc());
    Console.WriteLine("ToLongDateString() - "+dt.ToLongDateString());
    Console.WriteLine("dt.ToLongTimeString() - "+dt.ToLongTimeString());
    Console.WriteLine("dt.ToOADate() - "+dt.ToOADate());
    Console.WriteLine("dt.ToShortDateString() - "+dt.ToShortDateString());
    Console.WriteLine("dt.ToShortTimeString() - "+dt.ToShortTimeString());
    Console.WriteLine("dt.ToUniversalTime() - "+dt.ToUniversalTime());
    
    Ouput
    ToBinary() - 631115712000000000
    ToFileTime() - 126204282000000000
    ToLocalTime() - 05-12-2000 05:30:00
    ToFileTimeUtc() - 126204480000000000
    ToLongDateString() - 05 December 2000
    ToLongTimeString() - 00:00:00
    ToOADate() - 36865
    ToShortDateString() - 05-12-2000
    ToShortTimeString() - 00:00
    ToUniversalTime() - 04-12-2000 18:30:00
    


Write a comment


Name Email Message