tags:

views:

313

answers:

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"
+8  A: 

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
Love the John Skeet battle cry for help :D
Arcturus
Just want to note that you should also convert to UTC
Jeff Meatball Yang
+3  A: 

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);
Jason Punyon
Thanks! I completely forgot about substraction. I couldnt find TotalMilliseconds and was stuck to milliseconds.
acidzombie24
Just want to note that you should also be using UTC times.
Jeff Meatball Yang
+1  A: 

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
You should probably use `DateTime.UtcNow` or else convert the time to UTC before doing the subtraction.
Daniel Pryden
@Daniel: Quite right you are, fixed.
T.J. Crowder
A: 
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.

Ardman
You should be using `DateTime.UtcNow` or else call `dt.ToUniversalTime()`. Also, you have two constructor calls that are superfluous.
Daniel Pryden
Thanks for pointing that out. :o)
Ardman
+2  A: 

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
Your example won't compile. Perhaps you meant `double ticks = DateTime.UtcNow.UnixTicks();` ?
Daniel Pryden
Yes - thanks, I think I was stuck in Javascript!
Jeff Meatball Yang
A: 

How about

var s = string.format("{0}  milliseconds since 1970/01/01",
           (DateTime.Now - DateTime.Parse("1970/01/01")).TotalMilliseconds);
Charles Bretana
Why hardcode the date as a string? Also, you really should be doing the calculation in UTC.
Daniel Pryden
+2  A: 

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