Scott Booth's Blog
My Links
Blog Stats
  • Posts - 5
  • Stories - 0
  • Comments - 10
  • Trackbacks - 0
Archives

Monday, January 15, 2007

If you ever have a situation in which you need to convert a DateTime data type into hex, here's an easy way to do so:

If you do this conversion often, use constant:
   const long ticks1970 = 621355968000000000; // .NET ticks for 1970

This constant was generated with:
  DateTime dt70 = new DateTime( 1970, 1, 1, 0, 0, 0, 0 );
  long ticks1970 = dt70.Ticks;

get current time: 
 int gmt = (int) ((DateTime.UtcNow.Ticks - ticks1970 ) / 10000000L);
String hexDate = gmt.ToString(“X2“);

or you can convert back:

   int gmt = 0x3e482b89; // sample GMT time in seconds since 1970
   DateTime yourDateTime = new DateTime( ticks1970 + gmt * 10000000L );

posted @ 5:44 PM | Feedback (0)
Scott Booth