how to get milliSeconds since the Unix epoch in C# ?

G

Guest

Hi,

I do i get the difference, measured in milliseconds, between
the current time and midnight, January 1, 1970 UTC.
like in java System.currentTimeMillis() ?

Thanks.
 
A

Anders K. Olsen

yaron said:
Hi,

I do i get the difference, measured in milliseconds, between
the current time and midnight, January 1, 1970 UTC.
like in java System.currentTimeMillis() ?

You can use DateTime.Now.Ticks.

It will give you the number of 100 nanoseconds since 12:00 A.M., January 1,
0001, but you should be able to convert it to what you need.

Regards
Anders
 
J

Jon Skeet [C# MVP]

Anders said:
You can use DateTime.Now.Ticks.

It will give you the number of 100 nanoseconds since 12:00 A.M., January 1,
0001, but you should be able to convert it to what you need.

It's probably easier to use DateTime.Now-new DateTime(1970,1,1) and
then use the TotalMilliseconds property of the returned TimeSpan.

You might also want to consider using DateTime.UtcNow rather than
DateTime.Now - it depends on the exact application.

Jon
 
A

Anders K. Olsen

Jon Skeet said:
It's probably easier to use DateTime.Now-new DateTime(1970,1,1) and
then use the TotalMilliseconds property of the returned TimeSpan.

You might also want to consider using DateTime.UtcNow rather than
DateTime.Now - it depends on the exact application.

Of course, you are right.

I'm originally a Java programmer myself, and I have found that most of the
places where I use System.currenTimeMillis() was to calculate timespans. For
these cases I didn't realy care about the exact number, since I only needed
the difference. For this purpose, I have found that DateTime.Now.Ticks work
very well.

I just realized, that I may actually run into a problem, if I try to compare
times before and after a daylight savings time shift... I better use UtcNow
instead of just Now in the future.

Regards
Anders
 
G

Guest

Hi all,

i am using it for 2 clients , one is c# client and the other is java client.

the 2 clients send messages between them and use timeStamp.

the java client use the method System.currentTimeMillis() for timeStamp
which return the difference, measured in milliseconds, between the current
time and midnight, January 1, 1970 UTC.

what do i need to do in c# to work with the java timeStamp ?

Thanks.
 
M

Mark Nijhof

DateTime Yesterday = DateTime.Now.AddDays(-1);
TimeSpan TS = (TimeSpan)(DateTime.Now - Yesterday);
TS.TotalMilliseconds();

-Mark
 
J

Jon Skeet [C# MVP]

yaron said:
the java client use the method System.currentTimeMillis() for timeStamp
which return the difference, measured in milliseconds, between the current
time and midnight, January 1, 1970 UTC.

what do i need to do in c# to work with the java timeStamp ?

long millis = (DateTime.UtcNow-new DateTime (1970, 1, 1)).TotalMillis;

Jon
 
J

Jon Skeet [C# MVP]

Jon said:
long millis = (DateTime.UtcNow-new DateTime (1970, 1, 1)).TotalMillis;

Correction:
long millis = (DateTime.UtcNow-new DateTime (1970, 1,
1)).TotalMilliseconds;

Alternatively:
TimeSpan span = DateTime.UtcNow-new DateTime(1970, 1, 1);
long millis = span.TotalMilliseconds;

Or:
static readonly DateTime StartOfEpoch = new DateTime(1970, 1, 1);
....

long millis = (DateTime.UtcNow - StartOfEpoch).TotalMilliseconds;

Jon
 
S

Stoitcho Goutsev \(100\) [C# MVP]

yaron,

There is no such method or porperty, but you can calkulate it pretty easy:

DateTime baseTime = new DateTime(1970, 1, 1, 0, 0, 0);
DateTime nowInUTC = DateTime.UtcNow;
Console.WriteLine((nowInUTC - baseTime).Ticks / 10000);

HTH
Stoitcho Goutsev (100) [C# MVP]
 
G

Guest

Thanks you all.
the prefer way for me is what Stoitcho Goutsev wrote.


Stoitcho Goutsev (100) said:
yaron,

There is no such method or porperty, but you can calkulate it pretty easy:

DateTime baseTime = new DateTime(1970, 1, 1, 0, 0, 0);
DateTime nowInUTC = DateTime.UtcNow;
Console.WriteLine((nowInUTC - baseTime).Ticks / 10000);

HTH
Stoitcho Goutsev (100) [C# MVP]

yaron said:
Hi,

I do i get the difference, measured in milliseconds, between
the current time and midnight, January 1, 1970 UTC.
like in java System.currentTimeMillis() ?

Thanks.
 
J

Jon Skeet [C# MVP]

yaron said:
Thanks you all.
the prefer way for me is what Stoitcho Goutsev wrote.

I would suggest that rather than dividing by the literal 10000, you
divide by TimeSpan.TicksPerMillisecond - that will make it clearer what
you're doing.

(I personally prefer to use the TotalMilliseconds property, although
you then need to cast from double to long...)

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top