Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the time in millisecs to DateTime?

Tags:

c#

winapi

There are lots of ideas about this topic but this one is about Win32/WinEventProc's dwmsEventTime.

MSDN http://msdn.microsoft.com/en-us/library/windows/desktop/dd373885(v=vs.85).aspx states that it's the event time in milliseconds but no further explanations provided. Milliseconds from where?

Here is the sample value returned by that event:

dwmsEventTime: 1209382650 
DateTime.Now:  05/21/2014 16:49:37  (this should be very close to dwmsEventTime)

Does anyone know how to convert this parameter to .NET DateTime()?

like image 550
meraydin Avatar asked Sep 30 '22 18:09

meraydin


1 Answers

You might try something like:

static DateTime GetDateTimeFromMillisecondNumber(int millisecCount)
{
  return DateTime.Now.AddMilliseconds(millisecCount - Environment.TickCount);
}

But be aware that if the system was rebootet since the millisecCount was obtained, this returns rubbish. The same if this is run on a different machine from the one that you obtained the count from.

Also, this is only correct "modulo 49.7 days", i.e. the correct date and time might be an integral multiple of 49.7 days greater or less than what my method returns.

like image 53
Jeppe Stig Nielsen Avatar answered Oct 11 '22 07:10

Jeppe Stig Nielsen