Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

Convert PST to UTC using Moment?

Writer Emily Wong

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 ?

2 Answers

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
1

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy