简体   繁体   English

如何将 UTC 时间戳按原样导入 Luxon? (从瞬间迁移)

[英]how to import an UTC timestamp to Luxon as it is? (Migrating from Moment)

I have this line in my app:我的应用程序中有这一行:

const createdOn: moment.Moment = moment.utc(created_on)

created_on comes from an api endpoint like in this format: created_on来自一个像这种格式的 api 端点:

{ 
  ...,
  created_on: "2019-03-08T15:32:26.285Z",
}

This basically imports created_on as an UTC timezone.这基本上将created_on导入为 UTC 时区。 created_on is also UTC . created_on也是UTC So, this method does not break the timezone and import UTC properly.因此,此方法不会破坏时区并正确导入 UTC。 Also I have this one:我也有这个:

That generates current timestamp in UTC timezone.这会在 UTC 时区生成当前时间戳。

moment.utc()

Note that , If I just import date to moment and then convert it to UTC, my time goes wrong.请注意,如果我只是将日期导入到时刻,然后将其转换为 UTC,我的时间就会出错。 Moment by default assumes given date is equal to current visitors timezone.默认情况下,Moment 假定给定日期等于当前访问者时区。 I need to import time as it is.我需要按原样导入时间。 Which is UTC all the time.这一直是UTC。

What is the equivelant on Luxon ? Luxon 的Luxon什么?

You can use DateTime.utc and you can have a look at For Moment users section of Luxon's manual.您可以使用DateTime.utc ,也可以查看 Luxon 手册的For Moment users部分。

You can find in theCreation section:您可以在创建部分找到:

 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

So, if your input is a string, you can use from method (like fromISO ) using {zone: 'utc'} option因此,如果您的输入是字符串,则可以使用{zone: 'utc'}选项使用from方法(如fromISO

Here a live sample:这是一个实时示例:

 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM