Simpler way to get a full object when starting from Duration.fromMillis(…)?
See original GitHub issueHello 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:
- Created 5 years ago
- Comments:7 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
You can simplify that in other ways:
That’s easy enough in userland that I don’t want to add it to the library.
@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.