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






Add milliseconds to DateTime

    


using System;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {

        string dateFormat = "MM/dd/yyyy hh:mm:ss.fffffff"; 
        DateTime date1 = new DateTime(1999, 2, 2, 16, 0, 0);
        Console.WriteLine("Original date: {0} ({1:N0} ticks)",date1.ToString(dateFormat), date1.Ticks);
        
        DateTime date2 = date1.AddMilliseconds(1);
        Console.WriteLine("Second date:   {0} ({1:N0} ticks)",date2.ToString(dateFormat), date2.Ticks);
        Console.WriteLine("Difference between dates: {0} ({1:N0} ticks)",date2 - date1, date2.Ticks - date1.Ticks);                        

    }
}

/*

Original date: 02/02/1999 04:00:00.0000000 (630,535,680,000,000,000 ticks)
Second date:   02/02/1999 04:00:00.0010000 (630,535,680,000,010,000 ticks)
Difference between dates: 00:00:00.0010000 (10,000 ticks)

*/

   
    
    
    
  








Related examples in the same category

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