question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Simpler way to get a full object when starting from Duration.fromMillis(…)?

See original GitHub issue

Hello and thanks for the awesome library.

I was wondering if there’s a simpler way to produce an object with these values: (years, months, days, hours, minutes, seconds, milliseconds) when starting from Duration.fromMillis(…)

I noticed that:

Duration.fromMillis(22930346000).normalize().toObject();

will only return an object with milliseconds.

If i provide a full object to begin with, then it works as expected:

Duration.fromObject({
  years: 0,
  months: 0,
  days: 0,
  hours: 0,
  minutes: 0,
  seconds: 0,
  milliseconds: 22930346000,
}).normalize().toObject();

I just wanted to know if there’s something that i’m missing that would avoid passing a big object just to get back what i need. Thanks in advance!

PS: I made a jsfiddle if needed.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
icambroncommented, Sep 22, 2018

Yeah, what’s happening is that Luxon thinks of normalizing as something that happens across the units included in the duration. For example, you excluded weeks from your object and so the normalization rolled days directly up to months. But it has to know to do that somehow.

Here’s a shortcut:

luxon.Duration.fromMillis(22930346000).shiftTo('years', 'months', 'days', 'hours', 'minutes', 'seconds', 'milliseconds').toObject()

You can simplify that in other ways:

const fullUnits = ['years', 'months', 'days', 'hours', 'minutes', 'seconds', 'milliseconds'];
luxon.Duration.prototype.toFull = function() {return this.shiftTo.apply(this, fullUnits);}
luxon.Duration.fromMillis(22930346000).toFull().toObject();

That’s easy enough in userland that I don’t want to add it to the library.

1reaction
icambroncommented, Feb 7, 2020

@jeffal Ah, yes, toISO upconverts milliseconds to seconds because ISO can only represent the millis as decimal seconds, so there’s no other way to do it. So more of a side effect than anything else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Luxon, what is the best way to convert a duration into a ...
Use the toFormat function: https://moment.github.io/luxon/api-docs/index.html#durationtoformat.
Read more >
luxon 3.1.1 | Documentation
To create one from a custom string format, use DateTime.fromFormat. To create one from a native JS date, use DateTime.fromJSDate. Gregorian calendar and...
Read more >
Date/Time functions · JSONata
$fromMillis() ... Convert the number representing milliseconds since the Unix Epoch (1 January, 1970 UTC) to a formatted string representation of the timestamp...
Read more >
JavaScript | Converting milliseconds to date. - GeeksforGeeks
Example 1: This example first gets the milliseconds of the current date and time, Then using that value to get the date by...
Read more >
Date.prototype.getTime() - JavaScript - MDN Web Docs - Mozilla
The getTime() method returns the number of milliseconds since the epoch, ... You can use this method to help assign a date and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found