Get the number of ticks that represent the date and time of DateTime in CSharp
Description
The following code shows how to get the number of ticks that represent the date and time of DateTime.
Example
/*w w w . j av a 2 s. c o m*/
using System;
public class MainClass{
public static void Main(String[] argv){
DateTime centuryBegin = new DateTime(2001, 1, 1);
DateTime currentDate = DateTime.Now;
long elapsedTicks = currentDate.Ticks - centuryBegin.Ticks;
TimeSpan elapsedSpan = new TimeSpan(elapsedTicks);
System.Console.WriteLine(" {0:N0} nanoseconds", elapsedTicks * 100);
System.Console.WriteLine(" {0:N0} ticks", elapsedTicks);
System.Console.WriteLine(" {0:N2} seconds", elapsedSpan.TotalSeconds);
System.Console.WriteLine(" {0:N2} minutes", elapsedSpan.TotalMinutes);
System.Console.WriteLine(" {0:N0} days, {1} hours, {2} minutes, {3} seconds",
elapsedSpan.Days, elapsedSpan.Hours,
elapsedSpan.Minutes, elapsedSpan.Seconds);
}
}
The code above generates the following result.