Add TimeSpan to DateTime : Date Time Calculation « Date Time « C# / C Sharp






Add TimeSpan to DateTime

     

using System;
using System.Globalization;

class MainClass {
    public static void Main() {
        DateTime dt = new DateTime();
        DateTime dt1 = new DateTime(2001, 12, 31);
        DateTime dt2 = new DateTime(2000, 12, 31, 23, 59, 59);

        TimeSpan year = new TimeSpan(365 * TimeSpan.TicksPerDay);

        for (int i = 0; i < 10; ++i)
            dt += year;

        Console.WriteLine("DT as string: {0}", dt.ToString());
    }
}

   
    
    
    
  








Related examples in the same category

1.Add 2 month to the date time
2.use the Add() method to add a TimeSpan to a DateTime
3.use the overloaded addition operator (+) to add a TimeSpan to a DateTime
4.use the AddYears(), AddMonths(), AddDays(), AddMinutes(), and AddSeconds() methods to add periods to a DateTime
5.Measures the time taken to add some numbersMeasures the time taken to add some numbers
6.Add TimeSpan to a new DateTime
7.Add the specified number of days to a DateTime
8.Add hours to a DateTime
9.Add two DateTime value together
10.Add milliseconds to DateTime