Getting past and future dates using DateTime in C#

Now that you know how to use the DateTime structure, display date and time in different formats and how to get different date, month, year, hour, minute and seconds information for today’s date and current time lets teach you how to perform addition and subtraction of days, hours, minutes etc to get new date and time values. The DateTime structure makes this very easy with the

Adding and subtracting days

Using the DateTime structure you can add and subtracts days to any day. Look at the code below

class Program  {      static void Main(string[] args)      {          // Add 50 days to current day          DateTime dt = DateTime.Today.AddDays(50);            Console.WriteLine("The date 50 days from now will be");          Console.WriteLine("{0}", dt.Date.ToLongDateString());      }  }

Look how we are adding 50 days to current day and getting the new day information. Here we are creating a new object dt of the DateTime structure and storing the new date information in it. Please remember that because DateTime is a structure so dt is an object and not a pointer. This means that you cannot write DateTime dt = null; because dt is not a pointer. Below is the output of this code

Adding days using DateTime in C#
Now let’s see how we can subtract days from a given date. You would expect that there will be a Subtract method in the DateTime structure which you can use to subtract the days from a given day. There is a Subtract method but its use is somewhat different. It is used to get the difference between two date time values or two time spans. To subtract days from a given day you will be using the same AddDays method but will pass a negative value to it. Look at the code below

class Program  {      static void Main(string[] args)      {          // Subtract 50 days from current day          DateTime dt = DateTime.Today.AddDays(-50);            Console.WriteLine("Current date : " + DateTime.Today.ToLongDateString());          Console.WriteLine("The date 50 days ago was");          Console.WriteLine("{0}", dt.Date.ToLongDateString());      }  }

See how we have passed a negative value to the AddDays methods. Look at the output below

Subtracting days using DateTime in C#
Apart from AddDays you can also use the AddMonths and AddYears methods from the DateTime structure.

Adding and subtracting time using DateTime structure

Similar to adding and subtract days from a given day you can also add and subtract time from a give time. For this you have many methods available like AddHour, AddMinutes, AddSeconds. Look at the code below

class Program  {      static void Main(string[] args)      {          // Add 5 hours to current time          DateTime dt = DateTime.Now.AddHours(5);            // Subtract 5 hours from current time          DateTime dt1 = DateTime.Now.AddHours(-5);            // Add 30 minutes to current time          DateTime dt2 = DateTime.Now.AddMinutes(30);            // Subtract 30 minutes from current time          DateTime dt3 = DateTime.Now.AddMinutes(-30);            // Add 30 seconds to current time          DateTime dt4 = DateTime.Now.AddSeconds(30);            // Subtract 30 seconds from current time          DateTime dt5 = DateTime.Now.AddSeconds(-30);      }  }

Read the comments to understand the code. You can print the value and see the output of each operation.

Add and Subtract methods in DateTime structure

Apart from the Add methods which we have used above there are also two methods called Add and Subtract in the DateTime structure. We will not talk about these methods right now because to understand them you need an understanding of the TimeSpan structure which we will be covering in coming tutorials.

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