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

Question: Is there a package like human-interval but in reverse? #45

Open
erfanium opened this issue Jun 3, 2021 · 2 comments
Open

Comments

@erfanium
Copy link

erfanium commented Jun 3, 2021

e.g.

humanInterval(60 * 1000); // returns "one minute"
@kfatehi
Copy link

kfatehi commented Dec 26, 2022

I needed this too and so I copied @erfanium's code example into ChatGPT which provided this function:

function humanInterval(ms) {
  if (ms < 1000) return `${ms} milliseconds`;
  if (ms < 60000) return `${Math.round(ms / 1000)} seconds`;
  if (ms < 3600000) return `${Math.round(ms / 60000)} minutes`;
  if (ms < 86400000) return `${Math.round(ms / 3600000)} hours`;
  return `${Math.round(ms / 86400000)} days`;
}

Which I then used to wrap this package, like so:

const _humanInterval = require('human-interval');

function humanInterval(input) {
  if (typeof input === "number") {
    let ms = input;
    if (ms < 1000) return `${ms} milliseconds`;
    if (ms < 60000) return `${Math.round(ms / 1000)} seconds`;
    if (ms < 3600000) return `${Math.round(ms / 60000)} minutes`;
    if (ms < 86400000) return `${Math.round(ms / 3600000)} hours`;
    return `${Math.round(ms / 86400000)} days`;
  } else {
    return _humanInterval(input)
  }
}

This was a quick solution which worked well to realize the functionality.

@ianwalter
Copy link

https://github.com/EvanHahn/HumanizeDuration.js

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

3 participants