Convert date and time to numeric value in Javascript

Asked

Viewed 2,441 times

6

I saw in the Kibana dates are treated as numerical values.

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

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

2 answers

7


That’s probably timestamp which is the number 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 there just use the function getTime().

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

I put in the Github for future reference.

2

That numerical value (1462280400000) is the timestamp, which is the amount of time that has passed since the Unix Epoch (whereas the Unix Epoch corresponds to 1 January 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 have more accuracy, such as microseconds or even nanoseconds, see here some examples). In this case, the value 1462280400000 is in milliseconds, and in Javascript you can pass it directly to the constructor 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 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());

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

Already in the second case I passed directly the string '2016-05-03T10:00:00', that is in format ISO 8601 (that the builder of Date normally accepted), 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 that’s the way it is).


But there is a detail in the above codes: the results may vary depending on the Timezone (time zone) of browser - which usually uses what is configured in the operating system (in the case of Node.js, it is possible to configure it). That is, in the first code (new Date(1462280400000)), the date to 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 this same instant corresponds to a different date and/or time, depending on the Timezone. For example, the timestamp 1462280400000 corresponds to:

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

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

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

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 may correspond to a timestamp value different from 1462280400000, since in every part of the world this date and time occurred at different times).

If I’m not mistaken Chrome picks up the spindle that’s configured on the operating system, try changing it and running the above codes on 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 your choice. But if you want to be more specific, you can use the Moment js., together 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>

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 used Europe/Berlin, which corresponds to the German time zone.

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 time, when it was 10am in São Paulo, in Berlin it was 15h).

In the last 2 cases, the values of 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 one that best suits you.


Finally, it is always worth remembering that the transformation of a date and time into a timestamp - and vice versa - always involves some Timezone. Even if this is not explicit, there is some being used internally. There is no "magic".

Browser other questions tagged

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