2

I got a date in the below format: 2014-07-18

I need the unixTimeStamp of this date like PHP gives. I want to store those in a msAccess table which I can later sort on this timestamp value.

PHP returns this timestamp for the above date: 1405641600

How can I do this?

Thanks

jeet
  • 745
  • 3
  • 10
  • 15

2 Answers2

5

Usually i use this extension:

public static class Extensions
{
     public static  double ToUnixTime(this DateTime input)
     {
         return input.Subtract(new DateTime(1970,1,1)).TotalSeconds;
     }
}

As you mention in question, you need PHP like TimeStamp, so you will need to round TotalSeconds(it's double right now):

public static class Extensions
{
     public static int ToUnixTime(this DateTime input)
     {
         return (int)input.Subtract(new DateTime(1970,1,1)).TotalSeconds;
     }
}
Victor Leontyev
  • 8,488
  • 2
  • 16
  • 36
Uriil
  • 11,948
  • 11
  • 47
  • 68
4

Yep I think Anthony Chu is correct. I used the following site to check:

http://www.unixtimestamp.com/index.php

And it gave the answer 1405641600.

I also did the following C# to get the same answer for your example date:

var baseDate = new DateTime (1970, 01, 01);
var toDate = new DateTime (2014, 07, 18);
var numberOfSeconds = toDate.Subtract (baseDate).TotalSeconds;
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Ben Franklin
  • 626
  • 6
  • 21