Search code examples
datemilliseconds

Chrome is changing milliseconds to date in console


I'm trying to give a span element a id from milliseconds.

var span_id = new Date();
$('#query').append('<span id="'+span_id+'"></span><br>');

When i inspect the span element in the console it shows like this:

<span id="Tue Feb 09 2021 11:53:54 GMT+0100 (centraleuropeisk normaltid)"></span>

How can i save the time in milliseconds.. so that the span id will be a long number.. like: 1239484738329303

I've tried:

var d = new Date();
var n = d.getMilliseconds();

But this will only give me the milliseconds right now.. separated from the minutes, hours and date.. so it will end up short like: 281


Solution

  • This is not Chrome-only behaviour. Casting a date object to a string (which, by using it as an ID attribute, is what you're doing), well implicitly render it to its full textual representation.

    Instead, you need the static Date.now();

    Date.now(); //1612868588100