Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
Convert unix timestamp to specific timezone

I have an timestamp in unix like below which is in +2:00 timezone, but I want to get this same date but in +0:00 timezone, with using dayjs,

console.log("TIMESTAMP: ", dayjs.unix(timestamp).format());
>  TIMESTAMP:  2021-05-20T13:46:07+02:00

console.log("FIXED TIMESTAMP: ", dayjs.unix(timestamp).tz("Europe/London").format());
>  FIXED TIMESTAMP:  2021-05-20T12:46:07+01:00

above I try to do this with tz("Europe/London") but, I don't know why, I've got date in "+01:00" timezone..., Why this function do not return me new timestamp converted to "+0:00"?

Thanks for any help!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You have to include a couple of extender libraries for both UTC and Timezone formatting to work in dayjs. Both the UTC and Timezone Javascript files are required . I used version 1.10.7 because those were the most up to date.

If you're working in a browser include the following source. HTML:

<script src="https://unpkg.com/dayjs@1.10.7/dayjs.min.js"></script>
<script src="https://unpkg.com/dayjs@1.10.7/plugin/utc.js"></script>
<script src="https://unpkg.com/dayjs@1.10.7/plugin/timezone.js"></script>

JavaScript:

dayjs.extend(window.dayjs_plugin_utc);
dayjs.extend(window.dayjs_plugin_timezone);

let printFormat = 'hh:mm:ssA';
let nowLocal = dayjs().utc().local().format(printFormat);
console.log(nowLocal);
console.log(dayjs().tz("America/New_York").format(printFormat));
console.log(dayjs().tz("Asia/Tokyo").format(printFormat));

A couple of notes for your attempt, the 'tz' (Timezone) object does not hang off the unix() function, it is attached directly to the top level dayjs object. The above example takes the local time, and prints it in three different time zones each in the format hh:mm:ssAM|PM . The unix function takes an Unix timestamp and returns a DayJS object.

Here's a JSFiddle with the above example running: https://jsfiddle.net/e4x507p3/2/

Note, if you're using extensions in a browser, this should work, there is a different method required if you're working in NodeJS: https://day.js.org/docs/en/plugin/loading-into-nodejs

about 4 years ago · Juan Pablo Isaza Relatório

0

Have you tried this plug in?

dayjs().utc().format()

Here is a little sandbox.

Although it wont show you +0:00 timestamp, because it's presented in different ISO standard. Will that work for you?

about 4 years ago · Juan Pablo Isaza Relatório

0

import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat.js'
import utc from 'dayjs/plugin/utc.js'
import timezone from 'dayjs/plugin/timezone.js'
dayjs.extend(customParseFormat)
dayjs.extend(utc)
dayjs.extend(timezone)

const datetime = dayjs.unix(1653134400)

console.log(datetime.tz('UTC').format('YYYY-MM-DD HH:mm:ss Z')) // 2022-05-21 12:00:00 +00:00
console.log(
    datetime.tz('America/Los_Angeles').format('YYYY-MM-DD HH:mm:ss Z') // 2022-05-21 05:00:00 -07:00
)
console.log(datetime.tz('Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss Z')) // 2022-05-21 20:00:00 +08:00
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda