Javascript equivalent of "Date.now()" in C#?

Asked

Viewed 271 times

10

Here speaks how to get milliseconds using Date.now();, that returns something like 1533144170651.

Follows his documentation:

The method now() returns the milliseconds since January 1 1970 00:00:00 UTC so far as a Number.

How can I do this in C# (since January 1, 1970 00:00:00)? I tried this way:

int mil = DateTime.Now.Millisecond; // Retorna 898

4 answers

13


In recent versions you can use so:

using System;
using static System.Console;

public class Program {
    public static void Main() {
        WriteLine(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Documentation.

It is almost always used UtcNow. Usually you are representing the point in time, so there can be no variation no matter where it is used. You use universal time in the system and local time as presentation only. It only uses local time in the system if it needs it as a marking, something purely descriptive and not as a point in time. It gets to the point that if you need to use it, it’s best to use one string with the time since it is not a point in time.

The credit of the direct form goes to Guilherme Nascimento, I did not remember that had to access the clock of the machine direct for this class, still used an old form.

  • This code has returned since 1 January 1970 00:00:00 ?

  • Yes, same as JS. What can be checked in the links I posted.

  • Wow didn’t know that: ToUnixTimeSeconds().

  • @Matheusmiranda I was wrong, ToUnixTimeMilliseconds already makes all the adjustment, read enough things, and both using . utcNow and . now the result will be the same for ToUnixTimeMilliseconds, because probably the class internally already knows the Timezone that the server operates, so disregard my other comment.

5

The estate Now do C# brings the current date and time, different from JS Now.

The estate Millisecond returns an integer equivalent to milliseconds from the time Now was evaluated. It is clearer with the example below:

Console.WriteLine("Data com millisegundos: {0:MM/dd/yyy HH:mm:ss.fff}, {1}", 
                  DateTime.Now,DateTime.Now.Millisecond);

To create the same behavior in C# you should do so:

using System;

public class Program
{
    public static void Main()
    {

        DateTime d1 = new DateTime(1970, 1, 1);
        DateTime d2 = DateTime.Now;
        double x = new TimeSpan(d2.Ticks - d1.Ticks).TotalMilliseconds;
        double y =((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds();
        Console.WriteLine("Calculo com Ticks {0}",x);
        Console.WriteLine("Calculo com UtcNow {0}",y);      

    }
}

Note that the Ticks method has a slightly higher resolution than the Utcnow method.

See working on .NET Fiddle

4

It would not be simpler to use directly the .ToUnixTimeMilliseconds() in the DateTimeOffset.Now?

Thus:

  • Milliseconds:

    Console.WriteLine( DateTimeOffset.Now.ToUnixTimeMilliseconds() );
    
  • Seconds (to be interested to someone):

    Console.WriteLine( DateTimeOffset.Now.ToUnixTimeSeconds() );
    

Note that the only problem you may face with the method .ToUnixTimeMilliseconds() would be the question of using this in a lower version than 4.6 of .NET Framework (what would be problem of all answers made here so far).

Instead of doing the cast of DateTime.UtcNow for DateTimeOffset

Note:

Use DateTime.UtcNow.ToUnixTimeSeconds() or DateTime.Now.ToUnixTimeSeconds() seem to have the same effect for .ToUnixTimeSeconds() because probably the class knows the schedule and time zone and should compensate alone this in the return of the method.


Structure Datetimeoffset: https://msdn.microsoft.com/pt-br/library/system.datetimeoffset(v=vs.110). aspx

  • 1

    @Matheusmiranda so great, I did not say up to 4.6, I said lower than 4.6, ie 4.6 onwards works, 4.5 and earlier not, including all answers used ToUnixTimeSeconds, then you should understand that all have such a problem with old versions of net framework, but I think it’s now clear, anyway I just wanted to respond with something simpler and without having to do cast. You can use it there you’ll see it works.

  • I made the wrong comment

  • Here is version 4.7.2 :)

  • Face your answer is better than Maniero and less code.

  • @Matheusmiranda thank you!

  • Let me ask you, when the question was accepted, it can no longer be changed ?

  • @Matheusmiranda can yes, just click the button with the drawing so it is next to all answer (each answer has one), in doing this you exchange the answer you consider correct.

  • Both are correct, but yours is better than his. It is necessary ?

  • @Matheusmiranda I really prefer not to opine too much because it is your personal decision of what you solved and what you think best, if you consider this best can make the exchange at will and if you regret and want to go back you can change again ;)

  • Until when can I exchange answer as accepted ? No limit ?

  • 1

    @Matheusmiranda I think that any time you wish, just can not undo accept after a while, just do not remember "the limit"

Show 6 more comments

3

If you want a value similar to javascript can calculate the difference between the current date/time and 01/01/1970:

var d = Math.Floor((DateTime.Now.ToUniversalTime()- new DateTime(1970,1,1)).TotalMilliseconds);

See here working: https://dotnetfiddle.net/7G2Wpx

  • Almost Ricardo, is returning as double: 1533146367967.885.

  • Yes true, I used one Floor to solve this :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.