472,126 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

Java System.currentTimeMillis() equivalent

Does anyone know what the C# equivalent of System.currentTimeMillis()
would be?

Nov 1 '06 #1
9 32598
System.DateTime.Now.Millisecond

<Az******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Does anyone know what the C# equivalent of System.currentTimeMillis()
would be?

Nov 1 '06 #2
I think what you want is:

DateTime.Now.Ticks
Nov 1 '06 #3
And if you divide the number from Ticks by 10000 you get milliseconds.

so for example if what you wanted to do was time something, you could do:

long ticks = DateTime.Now.Ticks;
System.Threading.Thread.Sleep(20000);
Console.WriteLine((DateTime.Now.Ticks - ticks)/10000);

and it will correctly rprint 20000 (maybe plus a few milliseconds because of
the brief time it took to do the actual console.writeline)
Nov 1 '06 #4
no, this just returns the millisecond field of the current date time.

fallenidol wrote:
System.DateTime.Now.Millisecond

<Az******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Does anyone know what the C# equivalent of System.currentTimeMillis()
would be?
Nov 1 '06 #5
ah sorry. i was just guessing what currentTimeMillis did

<Az******@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
no, this just returns the millisecond field of the current date time.

fallenidol wrote:
>System.DateTime.Now.Millisecond

<Az******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googleg roups.com...
Does anyone know what the C# equivalent of System.currentTimeMillis()
would be?

Nov 1 '06 #6
MrNobody <Mr******@discussions.microsoft.comwrote:
And if you divide the number from Ticks by 10000 you get milliseconds.

so for example if what you wanted to do was time something, you could do:

long ticks = DateTime.Now.Ticks;
System.Threading.Thread.Sleep(20000);
Console.WriteLine((DateTime.Now.Ticks - ticks)/10000);

and it will correctly rprint 20000 (maybe plus a few milliseconds because of
the brief time it took to do the actual console.writeline)
However, you need to do extra translation to actually give the
equivalent of System.currentTimeMillis, as that is:

a) based on UTC
b) Since 1970, not 1AD

Here's an equivalent (tested by running a Java program and the C#
equivalent in close proximity - not tested for timezones as I'm in
GMT...)
static readonly DateTime Epoch = new DateTime (1970, 1, 1);
static long CurrentTimeMillis()
{
return (long)(DateTime.UtcNow-Epoch).TotalMilliseconds;
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 1 '06 #7

"MrNobody" wrote...
>I think what you want is:

DateTime.Now.Ticks
But it's not that simple...

There's at least three things to consider here:

1. DateTime.Ticks is in *100ds of nanoseconds*, not milliseconds.

2. A major difference between the Java way and the .NET way of handling
dates is that DateTime.Now yields a *local* date, while the Java Date
internally is a UTC date. This means that you want to use the
DateTime.UtcNow.Ticks instead.

3. But this isn't enough to compare with the milliseconds in the Java Date,
as Java and .NET uses different epochs; Java uses 1970-01-01, while .NET
uses 0001-01-01
/// Bjorn A


Nov 1 '06 #8
thanks guys,

I kind of combined both ideas and it seems to work.

DateTime UtcNow = DateTime.UtcNow.Ticks;
DateTime baseTime = new DateTime(1970, 1, 1, 0, 0, 0);
long timeStamp = (UtcNow - baseTime).Ticks / 10000;
Bjorn Abelli wrote:
"MrNobody" wrote...
I think what you want is:

DateTime.Now.Ticks

But it's not that simple...

There's at least three things to consider here:

1. DateTime.Ticks is in *100ds of nanoseconds*, not milliseconds.

2. A major difference between the Java way and the .NET way of handling
dates is that DateTime.Now yields a *local* date, while the Java Date
internally is a UTC date. This means that you want to use the
DateTime.UtcNow.Ticks instead.

3. But this isn't enough to compare with the milliseconds in the Java Date,
as Java and .NET uses different epochs; Java uses 1970-01-01, while .NET
uses 0001-01-01
/// Bjorn A
Nov 2 '06 #9
<Az******@gmail.comwrote:
I kind of combined both ideas and it seems to work.

DateTime UtcNow = DateTime.UtcNow.Ticks;
DateTime baseTime = new DateTime(1970, 1, 1, 0, 0, 0);
long timeStamp = (UtcNow - baseTime).Ticks / 10000;
Why rely on a constant which isn't terribly obvious when the
TotalMilliseconds makes it absolutely clear what the units returned
are?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 2 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by pawel | last post: by
14 posts views Thread by Lloyd Dupont | last post: by
1 post views Thread by neoedmund | last post: by
14 posts views Thread by mlw | last post: by
6 posts views Thread by tak | last post: by
350 posts views Thread by Lloyd Bonafide | last post: by
318 posts views Thread by King Raz | last post: by
7 posts views Thread by Sanny | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.