Pra*_*tha 2 javascript datetime momentjs ecmascript-6 luxon
我的应用程序中有这一行:
const createdOn: moment.Moment = moment.utc(created_on)
Run Code Online (Sandbox Code Playgroud)
created_on来自 API 端点,格式如下:
{
...,
created_on: "2019-03-08T15:32:26.285Z",
}
Run Code Online (Sandbox Code Playgroud)
这基本上created_on作为 UTC 时区导入。created_on也是UTC。因此,此方法不会破坏时区并正确导入 UTC。我还有这个:
这会生成 UTC 时区的当前时间戳。
moment.utc()
Run Code Online (Sandbox Code Playgroud)
请注意,如果我只是将日期导入到时刻,然后将其转换为 UTC,我的时间就会出错。默认情况下,时刻假定给定日期等于当前访问者时区。我需要按原样导入时间。始终是 UTC。
的等价物是什么Luxon?
您可以使用DateTime.utc并且可以查看Luxon 手册的“For Moment users”部分。
您可以在创建部分找到:
Run Code Online (Sandbox Code Playgroud)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'}选项
这是一个实时示例:
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
Run Code Online (Sandbox Code Playgroud)
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());Run Code Online (Sandbox Code Playgroud)