使用 Moment 将 PST 转换为 UTC?
Convert PST to UTC using Moment?
是否可以转换
太平洋时间 2022 年 2 月 7 日星期一 08:30:30 下午
使用 moment?
转换为 UTC 时间格式
我试过这样做
const endTime = timezone(filteredTimeLeft, "ddd, MMM DD, YYYY hh:mm:ss a").utcOffset(-8);'
但是Moment怎么知道插入的日期参数是太平洋时间呢?
您可以 read the documentation, which will lead you to Moment Timezone,Moment.js 的装饰器,它增加了对时区的支持。
但您可能还会注意到 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.
并且您可能会考虑使用更现代的库,例如 Luxon,由 Moment 的一位维护者编写,并由 Moment 团队维护。
时刻是EOL so I'll start with its successor luxon。
PST
是一个不明确的时区定义,因此不受支持。如果您想要准确的偏移量,请使用 UTC-8
;如果您想满足夏令时,请使用 IANA America/Los_Angeles
等。
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
一步到位
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
时刻相似,这是为什么 API 为 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
是否可以转换 太平洋时间 2022 年 2 月 7 日星期一 08:30:30 下午 使用 moment?
转换为 UTC 时间格式我试过这样做
const endTime = timezone(filteredTimeLeft, "ddd, MMM DD, YYYY hh:mm:ss a").utcOffset(-8);'
但是Moment怎么知道插入的日期参数是太平洋时间呢?
您可以 read the documentation, which will lead you to Moment Timezone,Moment.js 的装饰器,它增加了对时区的支持。
但您可能还会注意到 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.
并且您可能会考虑使用更现代的库,例如 Luxon,由 Moment 的一位维护者编写,并由 Moment 团队维护。
时刻是EOL so I'll start with its successor luxon。
PST
是一个不明确的时区定义,因此不受支持。如果您想要准确的偏移量,请使用 UTC-8
;如果您想满足夏令时,请使用 IANA America/Los_Angeles
等。
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
一步到位
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
时刻相似,这是为什么 API 为 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