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.

Add human durations from an object besides the provided object properties.

See original GitHub issue

It 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:closed
  • Created a year ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
KikoCosmeticscommented, Aug 12, 2022

@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!!

1reaction
linda-riancommented, Oct 7, 2022

@ricardocastanho shiftTo method allows to achieve the same result:

const duration = Duration.fromObject({ seconds: 1236836 }).shiftTo("days", "hours", "minutes", "seconds");
    
console.log(duration); // => "14 days, 7 hours, 33 minutes, 56 seconds"

But you would still need to filter out false values.

Read more comments on GitHub >

github_iconTop 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 >

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