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

228
Visualizações
Convert PST to UTC using Moment?

Is it possible to convert Mon, Feb 07, 2022 08:30:30 PM Pacific Time to UTC time format using moment?

I've tried doing this

const endTime = timezone(filteredTimeLeft, "ddd, MMM DD, YYYY hh:mm:ss a").utcOffset(-8);'

But how does Moment know the inserted date parameter is pacific time ?

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

0

Moment is EOL so I'll start with its successor luxon.

PST is an ambiguous definition of a timezone so not supported. Use UTC-8 if you meant the exact offset or an IANA America/Los_Angeles etc if you want to cater for day light savings.

const { DateTime } = require('luxon')
const filteredTimeLeft = "Mon, Feb 07, 2022 08:30:30 PM"

// Create a DateTime in the default zone
const local = DateTime.fromFormat(filteredTimeLeft, "EEE, MMM dd, yyyy hh:mm:ss a")
console.log(local.toLocaleString(DateTime.DATETIME_FULL))
//=> 7 February 2022, 8:30 pm GMT+1

// Set a custom zone, don't adjust the time
const local_set_pst = local.setZone("UTC-8", { keepLocalTime: true })
console.log(local_set_pst.toLocaleString(DateTime.DATETIME_FULL))
//=> 7 February 2022, 8:30 pm GMT-8

// Adjust to UTC
const pst_adjust_utc = local_set_pst.toUTC()
console.log(pst_adjust_utc.toLocaleString(DateTime.DATETIME_FULL))
//=> 8 February 2022, 4:30 am UTC

In one step

const pst_utc = DateTime.fromFormat(
  filteredTimeLeft,
  "EEE, MMM dd, yyyy hh:mm:ss a",
  { zone: "UTC-8" }
).toUTC()
console.log(pst_utc.toLocaleString(DateTime.DATETIME_FULL))
//=> 8 February 2022, 4:30 am UTC

Similar in moment, which is a good example of why the API changed for luxon:

const moment = require('moment-timezone')
const filteredTimeLeft = "Mon, Feb 07, 2022 08:30:30 PM"

// Read time, setting a zone
const endTime = moment.tz(filteredTimeLeft, "ddd, MMM DD, YYYY hh:mm:ss a", "America/Los_Angeles")
console.log(endTime.format("ddd, MMM DD YYYY, h:mm:ss a"))
// Mon, Feb 07 2022, 8:30:30 pm

// UTC
const utcEndTime = endTime.clone().utc()
console.log(utcEndTime.format("ddd, MMM DD YYYY, h:mm:ss a"))
// Tue, Feb 08 2022, 4:30:30 am

// be careful with moment though, if you don't clone(), endTime will be modified
endTime.utc()
console.log(endTime.format("ddd, MMM DD YYYY, h:mm:ss a"))
// Tue, Feb 08 2022, 4:30:30 am

about 4 years ago · Juan Pablo Isaza Relatório

0

You could read the documentation, which will lead you to Moment Timezone, a decorator for Moment.js that adds support for timezones.

But you also might notice that Moment.js is essentially deprecated:

We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done.

In practice, this means:

  • We will not be adding new features or capabilities.
  • We will not be changing Moment's API to be immutable.
  • We will not be addressing tree shaking or bundle size issues.
  • We will not be making any major changes (no version 3).
  • We may choose to not fix bugs or behavioral quirks, especially if they are long-standing known issues.

And that you might to consider using instead a more modern library such as Luxon, written by one of Moment's maintainers, and maintained by the Moment team.

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