Add human durations from an object besides the provided object properties.
See original GitHub issueIt would be nice if Luxon users can transform objects into a describing date, besides the provided object properties.
I would suggest a new property in ToHumanDurationOptions interface.
Actual behavior:
const obj = { minutes: 125 };
const duration = Duration.fromObject(obj).toHuman();
console.log(duration); // => "125 minutes"
Feature suggestion:
const obj = { minutes: 125 };
const opt = { style: "complete" };
const duration = Duration.fromObject(obj).toHuman(opt);
console.log(duration); // => "2 hours, 5 minutes"
Currently I did this:
const obj = { minutes: 125 };
const raw = Duration.fromObject(obj).toFormat('dd-hh-mm');
const [days, hours, minutes] = raw.split('-');
const dayLabel = days > 1 ? 'days' : 'day';
const dayText = days > 0 && `${parseInt(days)} ${dayLabel}`;
const hourLabel = hours > 1 ? 'hours' : 'hour';
const hourText = hours > 0 && `${parseInt(hours)} ${hourLabel}`;
const minutesLabel = minutes > 1 ? 'minutes' : 'minute';
const minutesText = minutes > 0 && `${parseInt(minutes)} ${minutesLabel}`;
const duration = [dayText, hourText, minuteText].filter(text => !!text).join(', ');
console.log(duration); // => "2 hours, 5 minutes"
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Chapter 7. Object arguments: functions working with objects
This chapter covers. Using objects as arguments; Accessing object properties from within a function; Adding new object properties from within a function ...
Read more >Data Structures: Objects and Arrays - Eloquent JavaScript
Both string and array values contain, in addition to the length property, a number of properties that hold function values.
Read more >Object.getOwnPropertyNames() - JavaScript - MDN Web Docs
The Object.getOwnPropertyNames() method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found ...
Read more >How to dynamically assign properties to an object in TypeScript
Solution 1: Explicitly type the object at declaration time · Solution 2: Use an object index signature · Solution 3: Use the Record...
Read more >Determining if all attributes on a javascript object are null or an ...
What is the most elegant way to determine if all attributes in a javascript object are either null or the empty string? It...
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
@ricardocastanho thanks for the workaround.
toFormat
saved my bacon!!I definitely don’t want to add the humanize library someone is suggesting. Luxon (TSLuxon in my case) should suffice!!
@ricardocastanho
shiftTo
method allows to achieve the same result:But you would still need to filter out false values.