Giter Site home page Giter Site logo

hengkiardo / humanizeduration.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from evanhahn/humanizeduration.js

0.0 2.0 0.0 200 KB

361000 becomes "6 minutes, 1 second"

Home Page: https://npmjs.org/package/humanize-duration

License: Do What The F*ck You Want To Public License

humanizeduration.js's Introduction

Humanize Duration

npm version build status

I have the time in milliseconds and I want it to become "30 minutes" or "3 days, 1 hour". Enter Humanize Duration!

Basic usage

This package is available as humanize-duration in Node and Bower. You can also include the JavaScript in the browser.

In the browser:

<script src="humanize-duration.js"></script>
<script>
humanizeDuration(12000)
</script>

In Node (or Browserify or Webpack or anywhere with CommonJS):

var humanizeDuration = require("humanize-duration")
humanizeDuration(12000)

Usage

By default, Humanize Duration will humanize down to the second, and will return a decimal for the smallest unit. It will humanize in English by default.

humanizeDuration(3000)      // "3 seconds"
humanizeDuration(2015)      // "2.25 seconds"
humanizeDuration(97320000)  // "1 day, 3 hours, 2 minutes"

You can change the settings by passing options as the second argument:

humanizeDuration(3000, { language: "es" })  // "3 segundos"
humanizeDuration(5000, { language: "ko" })  // "5 초"

humanizeDuration(22140000, { delimiter: " and " })  // "6 hours and 9 minutes"
humanizeDuration(22140000, { delimiter: "--" })     // "6 hours--9 minutes"

humanizeDuration(3600000, { units: ["hours"] })          // 1 hour
humanizeDuration(3600000, { units: ["days", "hours"] })  // 1 hour
humanizeDuration(3600000, { units: ["minutes"] })        // 60 minutes

humanizeDuration(3600000, {
  language: "es",
  units: ["minutes"]
})
// 60 minutos

Humanizers

If you find yourself setting same options over and over again, you can create a humanizer that changes the defaults, which you can still override later.

var spanishHumanizer = humanizeDuration.humanizer({
  language: "es",
  units: ["years", "months", "days"]
})

spanishHumanizer(71177400000)  // "2 años, 3 meses, 2 días"
spanishHumanizer(71177400000, { units: ["days", "hours"] })  // "823 días, 19.5 horas"

Internally, the main humanizeDuration function is just a wrapper around a humanizer.

Supported languages

Humanize Duration supports the following languages:

  • Catalan (ca)
  • Chinese, simplified (zh-CN)
  • Chinese, traditional (zh-TW)
  • Danish (da)
  • Dutch (nl)
  • English (en)
  • French (fr)
  • German (de)
  • Korean (ko)
  • Norwegian (nob)
  • Polish (pl)
  • Portuguese (pt)
  • Russian (ru)
  • Spanish (es)
  • Turkish (tr)

Credits

Lovingly made by Evan Hahn with language support by Martin Prins. Thanks to Filipi Siqueira for Portuguese support, Peter Rekdal Sunde for Norwegian support, Michał Janiec for Polish support, and Eileen Li for Chinese support.

Licensed under the WTFPL, so you can do whatever you want. Enjoy!

humanizeduration.js's People

Contributors

evanhahn avatar touhonoob avatar guidokessels avatar magarcia avatar dkavraal avatar nielssj avatar peters avatar mjjaniec avatar

Watchers

Hengki Sihombing avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.