Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeError: Missing relativeTo #29

Closed
steida opened this issue Feb 20, 2024 · 5 comments
Closed

RangeError: Missing relativeTo #29

steida opened this issue Feb 20, 2024 · 5 comments

Comments

@steida
Copy link

steida commented Feb 20, 2024

Is this code wrong?

const monthsSinceEpoch = value
  .since(Temporal.PlainDate.from("1970-01-01"))
  .total({ unit: "months" });

Another example:

const weeksSinceEpoch = Math.floor(
  getStartOfWeek(date)
    .toZonedDateTime(now.timeZoneId())
    .toInstant()
    .since(Temporal.Instant.fromEpochMilliseconds(0))
    // "weeks" throws `RangeError: Missing relativeTo` for some reason
    .total({ unit: "milliseconds" }) / millisecondsPerWeek,
);

Btw, thank you for the awesome lib!

@steida
Copy link
Author

steida commented Feb 20, 2024

Off-topic, for second example, I just realized I get the same with

const { weeks } = getStartOfWeek(date).since(
  getStartOfWeek(referenceDate),
  { largestUnit: "weeks" },
);

@arshaw
Copy link
Member

arshaw commented Feb 21, 2024

Hi @steida , that is part of the spec and is expected behavior... Temporal works with a number of different calendar systems (not just the Gregorian one that you and I are used to), so it requires a little more work. A Duration object on its own is not enough to know how many weeks/months total something is. You need to give it a reference point (aka "relativeTo"). Just give it the first date you have. Something like this:

const epoch = Temporal.PlainDate.from("1970-01-01");
const value = Temporal.Now.plainDateISO();
const monthsSinceEpoch = value.since(epoch).total({ unit: 'months', relativeTo: epoch });

@arshaw arshaw closed this as completed Feb 21, 2024
@arshaw
Copy link
Member

arshaw commented Feb 21, 2024

If you'd like to round it to a whole-number right away, you can do this instead:

const integerMonthsSinceEpoch = value.since(epoch, {
  largestUnit: 'month',
  smallestUnit: 'month'
}).months

@steida
Copy link
Author

steida commented Feb 21, 2024

Thank you! Am I correct that with getStartOfWeek, I don't need smallestUnit?

const { weeks } = getStartOfWeek(date).since(getStartOfWeek(epoch), {
  largestUnit: "weeks",
  // smallestUnit: "weeks",
});

@arshaw
Copy link
Member

arshaw commented Feb 21, 2024

If the getStartOfWeek function always returns dates that are aligned to the start-of-week, then yes, the returned duration should only ever have a 'weeks' value.

Just curious, where is this getStartOfWeek function coming from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants