Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the .NET framework support getting the current time in seconds based on UNIX timestamp?

Tags:

c#

.net

I am wondering if .NET contains a method to convert the current time in seconds or milliseconds to a UNIX timestamp (offset from 1970/1/1)?

like image 852
user705414 Avatar asked May 07 '11 02:05

user705414


2 Answers

you can get the ticks from 1970 (ie the UNIX timestamp) like this:

TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970,1,1,0,0,0);
double unixVersion = timeSpan.TotalSeconds;
like image 98
Mark Staff Avatar answered Oct 25 '22 12:10

Mark Staff


TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
Console.WriteLine((int)t.TotalSeconds);
like image 40
Matt Dearing Avatar answered Oct 25 '22 14:10

Matt Dearing