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>
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
Ferreira, this due to the GMT.
– Luiz Bortolo
Is there anything that can work around this failure using the package?
– Ferreira