0

I am trying to use luxon to generate a new date using a timezone. This is my code:

var luxon = require('luxon');
luxon.Settings.defaultZoneName = 'UTC+4';
var date = luxon.DateTime.local();
console.log(date);
var now = new Date(date.ts);
console.log(now.toString());

And this is the console:

DateTime {
    ts: 2018-09-13T13:09:45.333+04:00,
        zone: UTC+4,
        locale: en-US }
Thu Sep 13 2018 11:09:45 GMT+0200 (CEST)

But if I try to access the ts property like so

var date = luxon.DateTime.local();
console.log(date.ts); // here
var now = new Date(date.ts);
console.log(now.toString());

I get this in the console:

1536830052009
Thu Sep 13 2018 11:14:12 GMT+0200 (CEST)

Why is that? Is it doing some kind of math in the background? Also it turns out this date.ts is just ignoring my timezone. How can I fix that?

4

2 回答 2

1

First 1536830052009, This is your time in milliseconds,

new Date(1536830052009)
// output Thu Sep 13 2018 11:14:12 GMT+0200 (CEST)

You may want to check your timezone with getTimezoneOffset()

Returns the time difference between UTC time and local time, in minutes

Many people use moment.js to play with Date, I know it is not in your question but maybe you could find some usefull things

于 2018-09-13T09:21:26.857 回答
0

ts is not a public property and you shouldn't use it. Luxon does all sorts of tricks under the covers to get the math right. If you want the timestamp, just use date.toMillis(). If you want a JS Date, use date.toJSDate().

Two other important things to know:

  1. It's not ignoring your zone. The zone doesn't change the time. It's more like metadata about a time that affects how we display it. The Luxon docs cover this a bit. You shouldn't expect to extract a different timestamp by fiddling with the zone. Now is always now.
  2. Remember that the native Date object doesn't support timezones other than your local one. So anytime you convert from a Luxon object to a native Date, that information is lost. The time itself will be the same (meaning, it will represent the same millisecond), but it will express it in the local time.
于 2018-10-19T21:58:45.753 回答