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

8

Here talks about getting milliseconds using Date.now(); , which returns something like 1533144170651 .

Follow his documentation:

  

The now () method returns the milliseconds since 1 January   1970 as of 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
    
asked by anonymous 01.08.2018 / 19:38

4 answers

11

In recent versions you can use this:

using System;
using static System.Console;

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

See running on .NET Fiddle . And no Coding Ground . Also put it in GitHub for future reference .

Documentation .

UtcNow is almost always used. Usually you are representing the point in time, so you can not have variation no matter where it is used. You use the universal time in the system and the local time as the presentation only. It only uses the local time in the system if it needs it as a markup, something purely descriptive and not as a point in time. It gets to the point that if you need to use this it is best to use a 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 I had access to the clock of the machine direct by this class, still used an old form.

    
01.08.2018 / 19:41
4

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

The Millisecond property returns an integer equivalent to the milliseconds of the time Now was evaluated. It's 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 this:

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 method with UTCNow.

See working at .NET Fiddle

    
01.08.2018 / 20:25
3

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

So:

  • Milliseconds:

    Console.WriteLine( DateTimeOffset.Now.ToUnixTimeMilliseconds() );
    
  • Seconds (if interested):

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

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

Instead of casting from DateTime.UtcNow to DateTimeOffset

  

Note:

     

Using DateTime.UtcNow.ToUnixTimeSeconds() or DateTime.Now.ToUnixTimeSeconds() seems to have the same effect for .ToUnixTimeSeconds() because the class probably knows the time and time zone and should compensate this on method return alone.

DateTimeOffset structure: link

    
01.08.2018 / 21:23
2

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

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

See here working: link

    
01.08.2018 / 19:50