7

Basically i want to do the below in .NET but i have no idea how to.

var d = new Date().getTime() + " milliseconds since 1970/01/01"
  • BTW closely related to ... http://stackoverflow.com/questions/906034/calculating-future-epoch-time-in-c – cyberzed Apr 21 '10 at 14:20
  • 2
    The 6 answers I see including the one you accepted uses DateTime.Now instead of DateTime.UtcNow. I believe this will result in your computation being incorrect by +3,600,000 milliseconds more than half the year in the USA. This discrepancy may not matter in your particular case but something to be aware of. – JasDev Apr 21 '10 at 15:05
  • 1
    Actually, within local times in the USA, the error will permanently be more than that, as regardless whether DST is active or not, all timezones are far away from GMT/UTC. So, you're right, one *must* use DateTime.UtcNow. – Johan Boulé Dec 05 '11 at 06:21

8 Answers8

9

I'm not really sure you can get a UNIX date in .NET, but you have DateTime.Now as an equvivalent of new Date() (or new DateTime())

As you got in the comment, it's possible to get a TimeSpan object by doning something in the lines of...

(First answer)

DateTime.Now.Subtract(new DateTime(1970,1,1)).TotalMilliseconds

Adding the final result for the sake of mankind...

var d = DateTime.Now.Subtract(new DateTime(1970,1,1).ToUniversalTime()).TotalMilliseconds + " milliseconds since 1970/01/01";

P.S. Where is Jon Skeet with his knowledge of time when we need him :P

cyberzed
  • 2,026
  • 1
  • 16
  • 26
6

You'd do something like this...

var ts = DateTime.UtcNow - new DateTime(1970,1,1);
var result = String.Format("{0} milliseconds since 1970/01/01", ts.TotalMilliseconds);
Restore the Data Dumps
  • 38,967
  • 12
  • 96
  • 122
2

I wrote an extension method for myself a while back.

It's used like so:

 double ticks = DateTime.UtcNow.UnixTicks();

Implementation:

 public static class ExtensionMethods
 {
  // returns the number of milliseconds since Jan 1, 1970 
                // (useful for converting C# dates to JS dates)
  public static double UnixTicks(this DateTime dt)
  {
   DateTime d1 = new DateTime(1970, 1, 1);
   DateTime d2 = dt.ToUniversalTime();
   TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);

   return ts.TotalMilliseconds;
  }
 }
Jeff Meatball Yang
  • 37,839
  • 27
  • 91
  • 125
2

Subtraction is the way to do it, but all the responses I've seen so far do not correctly adjust for UTC.

You want something like:

var ts = DateTime.UtcNow - new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc); 
var result = String.Format("{0} milliseconds since 1970/01/01", ts.TotalMilliseconds);
Joe
  • 122,218
  • 32
  • 205
  • 338
1

You can get there via the DateTime and TimeSpan structures via DateTime.Subtract, something like:

TimeSpan ts = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1));
ts.TotalMilliseconds; // ...since The Epoch
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

How about

var s = string.format("{0}  milliseconds since 1970/01/01",
           (DateTime.Now - DateTime.Parse("1970/01/01")).TotalMilliseconds);
Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
0

I was fooling around and saw this example, but ended up modifying it to this:

var st = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToFileTimeUtc();
            var t = (DateTime.UtcNow.ToFileTimeUtc());
            Debug.WriteLine(t - st);

Will give a number like 16202########

-1
DateTime dt = new DateTime();
dt = DateTime.Now;
TimeSpan dtNow = new TimeSpan();
dtNow = dt.Subtract(new DateTime(1970, 1, 1));
Console.WriteLine(dtNow.TotalMilliseconds);

Bit long-winded in comparison to others, but it works.

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
  • You should be using `DateTime.UtcNow` or else call `dt.ToUniversalTime()`. Also, you have two constructor calls that are superfluous. – Daniel Pryden Apr 21 '10 at 16:10