The DateTime structure in C#

When working with C# applications you will very frequently need to work with the DateTime information. In C# there is a special structure called DateTime which you can use to access the system date and time information. This structure gives you everything you will need in relation to date and time, like the current date, current time, current time in hours, minutes and seconds, date and time format conversion, getting difference between two dates etc. In this tutorial you will get an introduction to this structure and then in the next tutorial you will learn how to use this structure to perform more operations.

Displaying the current date and time

Look at the code below

class Program  {      static void Main(string[] args)      {          Console.WriteLine("Time right now is : " + DateTime.Now);          Console.WriteLine("Today's date is : " + DateTime.Today);      }  }

Here we are printing two things on the console window. The first line prints the current time and the second line prints today’s date. The output for the program is as below

DateTime structure in C#
In this output notice the following

  1. The format of the date. You can see that the format of the date is month / day / year. You can choose to display the date in any format you wish to. You will learn how to do this in coming tutorials
  2. Both the outputs contain both date and time. This is because the DateTime structure will give you the information of both the date and the time by default.
  3. When we print DateTime.Now we get the time information also printed. But when we print DateTime.Today only the date information is printed. This is because Today means that you are asking for the date information and not the time information.

The common things you need from the DateTime structure are available as static members. To access the non static members you can create the object of the DateTime structure and access those members.

Important point before you start working with DateTime structure

Before you start working with DateTime structure please take a look at your operating system clock at the bottom right corner ( if you are using Windows operating system ) and see in which formats it displays the date and time information. The DateTime structure in C# will follow the date and time format of your system clock. So if your system clock shows the date in the format month / day / year then C# will also display it in the same format. And if your system clock shows the date in the format day / month / year then C# will also display it in this format. Mostly the format for date used in India is day / month / year and in the US is month / day / year.

Quick Links



Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..


Login to Open ESchool OR Create your account    Login   SignUp