Comments (4)
I am not 100% sure I am understanding your problem correctly.
You can already tell Luxon the zone of to use if the ISO string does not contain an offset:
DateTime.fromISO('2024-03-18T20:26:18.215', { zone: 'UTC+3' })
from luxon.
I tried your solution but I'm facing that the Date I provided is considered as UTC and when I add zone: ''UTC+3", it changes both the DateTime itself and the offset.
for example:
expirationDate: DateTime { ts: 2024-03-26T03:27:23.846+03:00, zone: Asia/Riyadh, locale: en-US },
now: DateTime { ts: 2024-03-26T00:43:14.915+03:00, zone: Asia/Riyadh, locale: en-US },
date: 2024-03-26T00:26:23.846Z,
expirationDateMillis: 1711412843846,
nowMillis: 1711402994915
and here's my function:
const isDateTimePassed = (date: Date, afterMins: number) => {
const expirationDate = DateTime.fromJSDate(date, {
zone: SATimezone,
}).plus({
minutes: afterMins,
});
const now = DateTime.now().setZone(SATimezone);
console.log({
expirationDate,
now,
date,
expirationDateMillis: expirationDate.toMillis(),
nowMillis: now.toMillis(),
flag: now > expirationDate,
flag2: now.toMillis() > expirationDate.toMillis(),
});
return now.toMillis() > expirationDate.toMillis();
};
afterMins in that case = 1
Also I tried convert it to isoString and use fromISO,
const expirationDate = DateTime.fromISO(date.toISOString(), {
zone: SATimezone,
}).plus({
minutes: afterMins,
});
from luxon.
A Date
(like a DateTime
) points to a specific point in time, represented by a unix timestamp. Passing zone
to fromJSDate
won't change this point in time, it just changes which time zone you're viewing it from.
An example:
const date = new Date('2024-01-01T10:00:00+00:00');
const dt1 = DateTime.fromJSDate(date, { zone: 'Europe/Berlin' });
const dt2 = DateTime.fromJSDate(date, { zone: 'America/New_York' });
// these two will show different offsets from UTC, but also different wall time, because they represent _the same instant in time_.
console.log(dt1.toISO(), dt2.toISO());
// you can verify this with toMillis:
console.log(dt1.toMillis(), dt2.toMillis());
In other words: No matter what you do, after calling fromJSDate
the following will always be true:
const date = /* whatever */;
const dt = DateTime.fromJSDate(date);
dt.toMillis() === date.valueOf(); // Always true
I think your misconception is that Date
represents day, month, year, hour, minute and seconds. It does not. It represents a unix timestamp.
I would highly suggest not working with Date
at all. Use Luxon all the way.
from luxon.
If you really want to do this, use the keepLocalTime
argument to setZone()
from luxon.
Related Issues (20)
- Adapter islamic and persian
- Able to pass invalid localWeekNumber
- DateTime.fromISO() returns invalid date for (some) numbers
- Timezone 'CST' is unsupported in Firefox browser HOT 1
- `twoDigitCutoffYear` not working as expected HOT 1
- Luxon fails to parse ISO 8601 date with 'Z' suffix as UTC HOT 1
- can't get week day index from UTC date HOT 2
- Property 'now' does not exist on type 'typeof DateTime'. HOT 1
- Wrong Time coming for Asia/Almaty timezone HOT 1
- Luxon seems incredibly illogical HOT 1
- Wrong output for Islamic calendar on Chrome Android HOT 2
- Duration::shiftTo does not convert to years correctly HOT 1
- Discrepancy on timezone short codes HOT 1
- Discrepancy on timestamp formatting when it comes to languages and countries / locale HOT 1
- Using `diff` leads to incorrect minute-of-day calculation due to Daylight Savings Time transition HOT 4
- DateTime.fromISO doesn't work with space, but javascript's Date.parse does HOT 4
- DateTime.fromISO does not work with lowercase 't' HOT 1
- Inconsistency in leap year difference of days HOT 1
- startOf() returning object with incorrect TimeZone offset HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from luxon.