Link to home
Start Free TrialLog in
Avatar of jonnyboy69
jonnyboy69

asked on

Timestamp in milliseconds

I need to create the following to integrate with a system:

"Date and time expressed as the number of milliseconds elapsed since January 1, 1970 00:00:00 GMT."

How would I do this assuming the time was DateTime.Now?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jonnyboy69
jonnyboy69

ASKER

Doesnt seem to be giving me the results I expected. For example I just did it and got 640. There have clearly ben more milliseconds than that from 1970?

string sTimeSpan = "";
                  DateTime d1 = new DateTime(1970, 1, 1);
                  DateTime d2 = DateTime.UtcNow;
                  TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
                  sTimeSpan = ts.Milliseconds.ToString();
Actually got it thanks, needed: ts.TotalMilliseconds
Hmm this is giving me incorrect results actualy

// Timespan
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = DateTime.Now;
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
sTimeStamp = ts.TotalMilliseconds.ToString();

I am getting for example:
1139977698718.75

Dont unerstand what the decimal point is about, surely milliseconds are lowest common denominator?

Thanks
The smallest unit of time is the tick, which is equal to 100 nanoseconds. That is why wne you convert to milliseconds, decimal gets in.