Coder Social home page Coder Social logo

Comments (4)

diesieben07 avatar diesieben07 commented on June 7, 2024 1

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.

MuAladdinIbrahim avatar MuAladdinIbrahim commented on June 7, 2024

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.

diesieben07 avatar diesieben07 commented on June 7, 2024

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.

icambron avatar icambron commented on June 7, 2024

If you really want to do this, use the keepLocalTime argument to setZone()

from luxon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.