最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to import an UTC timestamp to Luxon as it is? (Migrating from Moment) - Stack Overflow

programmeradmin1浏览0评论

I have this line in my app:

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

created_on es from an api endpoint like in this format:

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

This basically imports created_on as an UTC timezone. created_on is also UTC. So, this method does not break the timezone and import UTC properly. Also I have this one:

That generates current timestamp in UTC timezone.

moment.utc()

Note that, If I just import date to moment and then convert it to UTC, my time goes wrong. Moment by default assumes given date is equal to current visitors timezone. I need to import time as it is. Which is UTC all the time.

What is the equivelant on Luxon?

I have this line in my app:

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

created_on es from an api endpoint like in this format:

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

This basically imports created_on as an UTC timezone. created_on is also UTC. So, this method does not break the timezone and import UTC properly. Also I have this one:

That generates current timestamp in UTC timezone.

moment.utc()

Note that, If I just import date to moment and then convert it to UTC, my time goes wrong. Moment by default assumes given date is equal to current visitors timezone. I need to import time as it is. Which is UTC all the time.

What is the equivelant on Luxon?

Share Improve this question edited Nov 28, 2019 at 15:54 Dennis asked Nov 28, 2019 at 15:08 DennisDennis 1,9756 gold badges26 silver badges41 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You can use DateTime.utc and you can have a look at For Moment users section of Luxon's manual.

You can find in the Creation 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

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/npm/[email protected]/build/global/luxon.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.24.0/moment.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论