Convert date and time to numeric value in JavaScript

I have seen in Kibana the dates being treated as numeric values.

For example, the date 2016-05-03T10:00:00 is equal to the numeric value 1462280400000.

I would like to understand how this works, and what function it can convert.

Author: hkotsubo, 2016-05-03

2 answers

This is probably timestamp which is the amount of seconds since a specific date (1970-01-01 00:00:00 UTC), so it cannot represent any date. If that's what you want then just use the function getTime().

data = new Date(2016, 04, 03, 10, 30);
console.log(data.getTime());

I put on GitHub for future reference .

 7
Author: Maniero, 2020-10-23 18:23:55

This numeric value (1462280400000) is the timestamp , which is the amount of time that has passed since the Unix Epoch (whereas the Unix Epoch corresponds to January 1, 1970, at midnight, in UTC).

The most common is to see this value in seconds or milliseconds (depending on the language or API, and some may be more accurate, such as microseconds or even nanoseconds, see here some examples). In the case, or value 1462280400000 is in milliseconds, and in JavaScript you can pass it directly to the constructor of Date:

// converter o timestamp para uma data
let data = new Date(1462280400000);
console.log(data);

The opposite (get the timestamp corresponding to the date) can be obtained by the Method getTime:

// a partir de uma data, obter o timestamp
let data = new Date(2016, 4, 3, 10, 0);
console.log(data.getTime());

// também pode usar a string diretamente
data = new Date('2016-05-03T10:00:00');
console.log(data.getTime());

Notice that in the first case, the month is 4, because in JavaScript the months are indexed to zero (January is zero, February is 1, etc).

Already in the second case I passed the string directly '2016-05-03T10:00:00', which is in the format ISO 8601 (which the constructor of Date normally accepts), and in this case the month is 05 even (when it is a string, the months have the correct values: January is 1, February is 2, etc - confusing, but it's just like that).


But there is one detail in the codes above: the results may vary depending on the timezone (time zone) of the browser - which usually uses what is configured in the operating system (in the case of Node.js, is possible configure it ). That is, in the first code (new Date(1462280400000)), the date that will be printed by console.log may not be May 3, 2016 at 10:00, and in the second case the timestamp returned by getTime() may not be 1462280400000.

This is because the timestamp represents a single instant, a point in the timeline. Only that same moment corresponds to a different date and/or time, depending on the timezone. For example, the timestamp 1462280400000 corresponds to:

  • May 3, 2016 - 10: 00 in Brasilia Time
  • May 3, 2016 -15:00 in Berlin
  • 4 from May 2016 to 01: 00 in New Zealand

All the above dates and times correspond to the timestamp 1462280400000 - they all correspond to the same moment, the same point in the timeline(at the same time it is May 3 at 10 am in São Paulo, New Zealand it is already Day 4 at one in the morning).

Then depending on the timezone that is configured, the returned values can be completely different. In the first case, when passing the timestamp to the constructor of Date, the date displayed by console.log will be according to this timezone.

Already in the second case (passing the date and time values, or the string), the constructor will consider the moment corresponding to May 3, 2016, at 10 am, in the browser timezone (which in turn can correspond to a timestamp value different from 1462280400000, since in each part of the world this date and time will be time occurred at different times).

if I'm not mistaken Chrome takes the spindle that is configured in the operating system, try changing it and running the above codes in the browser console to see the difference.


Moment.js

Unfortunately JavaScript is limited to the browser timezone, and there is no way to specify any other to choose from. But if you want to be more specific, you can use Moment.js , along with the Moment Timezone :

// obter data a partir do timestamp, usando um timezone específico
let data = moment.tz(1462280400000, "America/Sao_Paulo");
console.log(data.format()); // 2016-05-03T10:00:00-03:00

// o mesmo timestamp, mas em outro timezone
data = moment.tz(1462280400000, "Europe/Berlin");
console.log(data.format()); // 2016-05-03T15:00:00+02:00

//-----------------------------------------------------
// uma data e hora, em um timezone específico
data = moment.tz("2016-05-03T10:00:00", "America/Sao_Paulo");
console.log(data.valueOf()); // obter o timestamp: 1462280400000

// a mesma data e hora, mas em outro timezone
data = moment.tz("2016-05-03T10:00:00", "Europe/Berlin");
console.log(data.valueOf()); // timestamp diferente do valor acima: 1462262400000
<script src="http://momentjs.com/downloads/moment.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data.js"></script>

In this way, you can explicitly say that the date and time refer to a specific timezone, regardless of what is configured in the browser. In the example above I used America/Sao_Paulo, which corresponds to the time of Brasilia, and then I used Europe/Berlin, which corresponds to the time zone of Germany.

In the first 2 cases, I used the same timestamp, but in different timezones, and the result was the corresponding date and time in each of these zones (note that the time is different, because at this moment, when it was 10h in São Paulo, in Berlin it was already 15h).

Already in the last 2 cases, the values of the timestamps are different, because "May 3, 2016 at 10 AM" occurred at different times in each of these timezones.

These timezone names in the format Continente/Região are defined by the IANA time Zone Database. If you want to see all available timezones, use moment.tz.names() and choose the which is more suitable for your case.


Finally, it is always worth remembering that the transformation of a date and time into a timestamp - and vice versa-always envelops some timezone. Even if this is not explicit, it has some being used internally. There's no magic.

 2
Author: hkotsubo, 2020-01-29 18:45:02