Get the unix timestamp in C#

Unix timestamp is the count of total seconds since 1970-01-01 to the current time. This number does not depend on the location where you’re executing the code. So, no worries about the timezone.

It’s very easy to get Unix timestamp from C#. You can use DateTime.UtcNow and subtracting for the Epoch time of 1970-01-01.

1
long myTimestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
long myTimestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

 

About The Author

Hao Nguyen

Hao Nguyen

Hello! I'm Hao Nguyen and I'm currently working as a Network Engineer for a small firm in Houston. I enjoy writing technical documents and blog, such as PlanTutorials.com

Leave a Reply