如何按原样将 UTC 时间戳导入 Luxon? (从 Moment 迁移)
how to import an UTC timestamp to Luxon as it is? (Migrating from Moment)
我的应用程序中有这一行:
const createdOn: moment.Moment = moment.utc(created_on)
created_on
来自 api 端点,格式如下:
{
...,
created_on: "2019-03-08T15:32:26.285Z",
}
这基本上是将 created_on
导入为 UTC 时区。 created_on
也是 UTC
。因此,此方法不会破坏时区并正确导入 UTC。我也有这个:
生成 UTC 时区的当前时间戳。
moment.utc()
注意,如果我只是将日期导入时刻,然后将其转换为UTC,我的时间就会出错。 Moment 默认假定给定日期等于当前访问者时区。我需要按原样导入时间。一直都是 UTC。
Luxon
的等效项是什么?
您可以使用 Luxon 手册的 DateTime.utc
and you can have a look at For Moment users 部分。
您可以在 Creation 部分找到:
Operation | Moment | Luxon | Notes
------------------------------------------------------------------------------------
From UTC civil time | moment.utc(Array) | DateTime.utc(Number...) | Moment also uses moment.utc() to take other arguments. In Luxon, use the appropriate method and pass in the { zone: 'utc'} option
因此,如果您的输入是字符串,您可以使用 from
方法(如 fromISO
)使用 {zone: 'utc'}
选项
这是一个活生生的例子:
const DateTime = luxon.DateTime;
const nowLuxon = DateTime.utc();
console.log(nowLuxon.toISO(), nowLuxon.toMillis());
const nowMoment = moment.utc();
console.log(nowMoment.format(), nowLuxon.valueOf());
const created_on = "2019-03-08T15:32:26.285Z";
const createdOnLuxon = DateTime.fromISO(created_on, { zone: 'utc'});
console.log(createdOnLuxon.toISO(), createdOnLuxon.toMillis());
const createdOnMoment = moment.utc(created_on);
console.log(createdOnMoment.format(), createdOnMoment.valueOf());
<script src="https://cdn.jsdelivr.net/npm/luxon@1.21.3/build/global/luxon.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
我的应用程序中有这一行:
const createdOn: moment.Moment = moment.utc(created_on)
created_on
来自 api 端点,格式如下:
{
...,
created_on: "2019-03-08T15:32:26.285Z",
}
这基本上是将 created_on
导入为 UTC 时区。 created_on
也是 UTC
。因此,此方法不会破坏时区并正确导入 UTC。我也有这个:
生成 UTC 时区的当前时间戳。
moment.utc()
注意,如果我只是将日期导入时刻,然后将其转换为UTC,我的时间就会出错。 Moment 默认假定给定日期等于当前访问者时区。我需要按原样导入时间。一直都是 UTC。
Luxon
的等效项是什么?
您可以使用 Luxon 手册的 DateTime.utc
and you can have a look at For Moment users 部分。
您可以在 Creation 部分找到:
Operation | Moment | Luxon | Notes ------------------------------------------------------------------------------------ From UTC civil time | moment.utc(Array) | DateTime.utc(Number...) | Moment also uses moment.utc() to take other arguments. In Luxon, use the appropriate method and pass in the { zone: 'utc'} option
因此,如果您的输入是字符串,您可以使用 from
方法(如 fromISO
)使用 {zone: 'utc'}
选项
这是一个活生生的例子:
const DateTime = luxon.DateTime;
const nowLuxon = DateTime.utc();
console.log(nowLuxon.toISO(), nowLuxon.toMillis());
const nowMoment = moment.utc();
console.log(nowMoment.format(), nowLuxon.valueOf());
const created_on = "2019-03-08T15:32:26.285Z";
const createdOnLuxon = DateTime.fromISO(created_on, { zone: 'utc'});
console.log(createdOnLuxon.toISO(), createdOnLuxon.toMillis());
const createdOnMoment = moment.utc(created_on);
console.log(createdOnMoment.format(), createdOnMoment.valueOf());
<script src="https://cdn.jsdelivr.net/npm/luxon@1.21.3/build/global/luxon.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>