Tejaswi Yerukalapudi Tejaswi Yerukalapudi - 1 month ago 19
C# Question

DateTime's representation in milliseconds?

I have a SQL-server timestamp that I need to convert into a representation of time in milliseconds since 1970. Can I do this with plain SQL? If not, I've extracted it into a

DateTime
variable in C#. Is it possible to get a millisec representation of this ?

Thanks,

Teja.

Answer

You're probably trying to convert to a UNIX-like timestamp, which are in UTC:

yourDateTime.ToUniversalTime().Subtract(
    new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
    ).TotalMilliseconds

This also avoids summertime issues, since UTC doesn't have those.