I can’t get the right timestamp time

Asked

Viewed 68 times

0

I have an API I’m consuming and in it I have the field timestamp worthwhile 1604327859 and with that value I have the following time and date:

inserir a descrição da imagem aqui

This value above is correct, I am using the Moment.js package, but the value that is returning me is 1970-01-19T10:38:47-03:00 and you’re wrong.

The execution of the code is as follows:

var moment = require('moment'); // require
console.log(moment(1604327859).format())

2 answers

1

The value of timestamp can be in different time units. The most common is to be in seconds or milliseconds, and each API/language usually works with one of them.

In the case of Moment.js, it works with the value in milliseconds, but the value you have is in seconds.

So either you multiply this value by 1000, or you use moment.unix, that receives the value in seconds.

Another detail is that format, by default, returns the date and time referring to the time zone of the environment in which the code runs (be the browser or Node settings, for example - see here, here and here for more details).

How do you want the date/time on UTC, can use utc() in the moment thus created format() return date/time in UTC:

console.log(moment.unix(1604327859).utc().format());
console.log(moment(1604327859 * 1000).utc().format());
<script src="https://momentjs.com/downloads/moment.min.js"></script>

Both will print "2020-11-02T14:37:39Z".


Just remembering that, technically, "2020-11-02T11:37:39-03:00" is not wrong, after all a timestamp represents a single instant, a point in the timeline, but may correspond to different dates/times depending on the Timezone (time zone). Just consider for example the "now". At this very moment, in every part of the world - in every time zone - the current date and time may be different, but the instant (the value of the timestamp) is the same for everyone.

Thus, the timestamp 1604327859 represents a single absolute instant, and it corresponds to both "2020-11-02T11:37:39-03:00" (11 am Eastern Standard Time) and "2020-11-02T14:37:39Z" (14h UTC). The -03:00 is the offset (the difference with respect to UTC, and in the case, indicates that the date/time in question is 3 hours behind UTC, and therefore both correspond to the same instant - to the same timestamp).


Another option is to use the Moment Timezone:

console.log(moment.tz(1604327859 * 1000, "UTC").format()); // 2020-11-02T14:37:39Z
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data.js"></script>

Note that the timestamp should be in milliseconds, and it is possible to inform the Timezone (in this case may be "UTC"), thus format() will not use the environment spindle.

This way is preferable if you need to convert to other timezones besides UTC. Ex:

let ts = 1604327859 * 1000;
// timezone do Japão
console.log(moment.tz(ts, "Asia/Tokyo").format()); // 2020-11-02T23:37:39+09:00
// timezone da Alemanha
console.log(moment.tz(ts, "Europe/Berlin").format()); // 2020-11-02T15:37:39+01:00
// Horário de Brasília
console.log(moment.tz(ts, "America/Sao_Paulo").format()); // 2020-11-02T11:37:39-03:00
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data.js"></script>

0

This return happens due to the date format you are sending, unixtimestamp (more info: https://www.unixtimestamp.com/). Try to adjust your code to:

var moment = require('moment'); // require
console.log(moment.unix(1604327859).format())

So you specify for Moment that the date input format is unixtimestamp.

  • The answer has not yet come out completely complete, the time has gone wrong, only the date is correct 2020-11-02T11:37:39-03:00

  • Ferreira, this due to the GMT.

  • Is there anything that can work around this failure using the package?

Browser other questions tagged

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