Add and subtract datetime in .Net

Suppose that you have two date time values one is startdate and second is EndDate, and you want to calculate time interval between both dates ,in this situation we can use timespan object like this:

(According to MSDN:  A TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date. Otherwise, the DateTime or DateTimeOffset  structure should be used instead.)
before starting of code


  1.         Dim StartTime As DateTime = #6/12/2008 3:09:00 PM#
  2.         Dim EndTime As DateTime = #6/10/2008 12:04:00 PM#
  3.         Dim TimeDiff As TimeSpan = EndTime.Subtract(StartTime)
  4.         Dim Days As Integer = TimeDiff.ToatlDays         
  5.                         'calculate Value of Days Component
  6.         Dim Minutes As Integer = TimeDiff.TotalMinutes     
  7.                         'calculate Value of Minutes Component
  8.         Dim Seconds As Integer = TimeDiff.TotalSeconds   
  9.                          'calculate Value of Seconds Component
  10.         Dim TotalDays As Integer = TimeDiff.TotalDays    
  11.                          'calculate Value of Total Days
  12.         Dim TotalHours As Integer = TimeDiff.TotalHours 
  13.                            'calculate Value of Total Minutes
  14.         Dim TotalSeconds As Integer = TimeDiff.TotalSeconds
  15.                            'calculate Value of Total Seconds

        we can use subtract method for calculating difference between two date like :

  1.         TimeDiff = EndTime.Subtract(StartTime)

Add a day, a month and a year to a Date

  2.             Dim FirstDate As Date
  3.             Dim SecondDate As Date
  4.            FirstDate = #2/28/2010#
  5.             'Add a day
  6.             SecondDate = FirstDate.AddDays(1)
  7.             'Add some months
  8.             SecondDate = FirstDate.AddMonths(6)
  9.             'Subtract a year
  10.             SecondDate = FirstDate.AddYears(-1)</blockquote>


  2.       DateTime StartTime = 12/06/2008 15:09:00;
  3. DateTime EndTime = 10/06/2008 12:04:00;
  4. TimeSpan TimeDiff = EndTime.Subtract(StartTime);
  5. int Days = TimeDiff.ToatlDays;
  6. //calculate Value of Days Component
  7. int Minutes = TimeDiff.TotalMinutes;
  8. //calculate Value of Minutes Component
  9. int Seconds = TimeDiff.TotalSeconds;
  10. //calculate Value of Seconds Component
  11. int TotalDays = TimeDiff.TotalDays;
  12. //calculate Value of Total Days
  13. int TotalHours = TimeDiff.TotalHours;
  14. //calculate Value of Total Minutes
  15. int TotalSeconds = TimeDiff.TotalSeconds;
  16. //calculate Value of Total Seconds
  17. //we can use subtract method for calculating difference between two date like :
  18. TimeDiff = EndTime.Subtract(StartTime);

Add a day, a month and a year to a Date

  2. System.DateTime SecondDate = default(System.DateTime);
  3. FirstDate = 28/02/2010 00:00:00;
  4. //Add a day
  5. SecondDate = FirstDate.AddDays(1);
  6. //Add some months
  7. SecondDate = FirstDate.AddMonths(6);
  8. //Subtract a year
  9. econdDate = FirstDate.AddYears(-1);


One thought on “Add and subtract datetime in .Net”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>