Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET: Get milliseconds since 1/1/1970

I have an ASP.NET, VB.NET Date, and I'm trying to get the number of milliseconds since January 1st, 1970. I tried looking for a method in MSDN, but I couldn't find anything. Does anyone know how to do this?

like image 655
Sebolains Avatar asked Apr 15 '11 17:04

Sebolains


1 Answers

You can subtract any two DateTime instances and get TimeSpan and TotalMilliseconds would give you total milliseconds. Sample below.

    DateTime dt1970 = new DateTime(1970, 1, 1);     DateTime current = DateTime.Now;//DateTime.UtcNow for unix timestamp     TimeSpan span = current - dt1970;     Console.WriteLine(span.TotalMilliseconds.ToString()); 

one liner

//DateTime.MinValue is 01/01/01 00:00 so add 1969 years. to get 1/1/1970 DateTime.Now.Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds; 
like image 60
Sanjeevakumar Hiremath Avatar answered Oct 02 '22 21:10

Sanjeevakumar Hiremath