Add DateTime and specified number of milliseconds in CSharp

Description

The following code shows how to add DateTime and specified number of milliseconds.

Example


/*www . j av a  2 s  .  com*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    string dateFormat = "MM/dd/yyyy hh:mm:ss.fffffff"; 
    DateTime date1 = new DateTime(2010, 9, 8, 16, 0, 0);
    System.Console.WriteLine("Original date: {0} ({1:N0} ticks)\n",
                      date1.ToString(dateFormat), date1.Ticks);
    
    DateTime date2 = date1.AddMilliseconds(1);
    System.Console.WriteLine("Second date:   {0} ({1:N0} ticks)",
                      date2.ToString(dateFormat), date2.Ticks);
    System.Console.WriteLine("Difference between dates: {0} ({1:N0} ticks)\n",
                      date2 - date1, date2.Ticks - date1.Ticks);                        
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var