C# DateTime AddMilliseconds

Description

DateTime AddMilliseconds returns a new DateTime that adds the specified number of milliseconds to the value of this instance.

Syntax

DateTime.AddMilliseconds has the following syntax.


public DateTime AddMilliseconds(
  double value
)

Parameters

DateTime.AddMilliseconds has the following parameters.

  • value - A number of whole and fractional milliseconds. The value parameter can be negative or positive. Note that this value is rounded to the nearest integer.

Returns

DateTime.AddMilliseconds method returns An object whose value is the sum of the date and time represented by this instance and the number of milliseconds represented by value.

Example

The following example uses the AddMilliseconds method to add one millisecond and 1.5 milliseconds to a DateTime value.


using System;//  w ww . ja  v  a  2 s.  com
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 »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version