user489041
user489041

Reputation: 28304

Get Epoch in C# for GMT

I need to get number of milliseconds from the epoch in GMT.

Can I use this for the GMT part:

DateTime.Now.ToUniversalTime()

What about the number of milliseconds since the epoch?

Upvotes: 3

Views: 3726

Answers (2)

Irshad
Irshad

Reputation: 3131

You can do as easily as below in newer .NET versions;

DateTimeOffset.Now.ToUnixTimeMilliseconds()

Refer docs.

Upvotes: 0

Eoin Campbell
Eoin Campbell

Reputation: 44278

This should give you a TimeZone agnostic answer.

TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
double ms = t.TotalMilliseconds ;

Upvotes: 8

Related Questions