npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@navy.gif/timestring

v6.0.6

Published

Parse a human readable time string into a time based value

Downloads

47

Readme

timestring

Version Build Status Coveralls npm License

Parse a human readable time string into a time based value.

Installation

npm install --save @navy.gif/timestring

or

yarn add @navy.gif/timestring

Usage

Overview

const timestring = require('@navy.gif/timestring')

let str = '1h 15m'
let time = timestring(str)

console.log(time) // will log 4500

By default the returned time value from timestring will be in seconds.

The time string can contain as many time groups as needed:

const timestring = require('@navy.gif/timestring')

let str = '1d 3h 25m 18s'
let time = timestring(str)

console.log(time) // will log 98718

and can be as messy as you like:

const timestring = require('@navy.gif/timestring')

let str = '1 d    3HOurS 25              min         1   8s'
let time = timestring(str)

console.log(time) // will log 98718

Keywords

timestring will parse the following keywords into time values:

  1. ms, milli, millisecond, milliseconds - will parse to milliseconds
  2. s, sec, secs, second, seconds - will parse to seconds
  3. m, min, mins, minute, minutes - will parse to minutes
  4. h, hr, hrs, hour, hours - will parse to hours
  5. d, day, days - will parse to days
  6. w, week, weeks - will parse to weeks
  7. mo, mon, mth, mths, month, months - will parse to months
  8. y, yr, yrs, year, years - will parse to years

Keywords can be used interchangeably:

const timestring = require('@navy.gif/timestring')

let str = '1day 15h 20minutes 15s'
let time = timestring(str)

console.log(time) // will log 141615

Return Time Value

By default the return time value will be in seconds. This can be changed by passing one of the following strings as an argument to timestring:

  1. ms - Milliseconds
  2. s - Seconds
  3. m - Minutes
  4. h - Hours
  5. d - Days
  6. w - Weeks
  7. mth - Months
  8. y - Years
const timestring = require('@navy.gif/timestring')

let str = '22h 16m'

let hours = timestring(str, 'h')
let days = timestring(str, 'd')
let weeks = timestring(str, 'w')

console.log(hours) // will log 22.266666666666666
console.log(days) // will log 0.9277777777777778
console.log(weeks) // will log 0.13253968253968254

Optional Configuration

A few assumptions are made by default:

  1. There are 24 hours per day
  2. There are 7 days per week
  3. There are 4 weeks per month
  4. There are 12 months per year
  5. There are 365.25 days per year

These options can be changed by passing an options object as an argument to timestring.

The following options are configurable:

  1. hoursPerDay
  2. daysPerWeek
  3. weeksPerMonth
  4. monthsPerYear
  5. daysPerYear
const timestring = require('@navy.gif/timestring')

let str = '1d'
let opts = {
  hoursPerDay: 1
}

let time = timestring(str, 'h', opts)

console.log(time) // will log 1

In the example above hoursPerDay is being set to 1. When the time string is being parsed, the return value is being specified as hours. Normally 1d would parse to 24 hours (as by default there are 24 hours in a day) but because hoursPerDay has been set to 1, 1d will now only parse to 1 hour.

This would be useful for specific application needs.

Example - Employees of my company work 7.5 hours a day, and only work 5 days a week. In my time tracking app, when they type 1d i want 7.5 hours to be tracked. When they type 1w i want 5 days to be tracked etc.

const timestring = require('@navy.gif/timestring')

let opts = {
  hoursPerDay: 7.5,
  daysPerWeek: 5
}

let hoursToday = timestring('1d', 'h', opts)
let daysThisWeek = timestring('1w', 'd', opts)

console.log(hoursToday) // will log 7.5
console.log(daysThisWeek) // will log 5

It is important to note that the daysPerYear configuration option will be used to convert a month or year to seconds, so if you are using custom configuration options make sure that you adjust this value to suit if you expect to be parsing timestrings containing months or years.

Notes

If the string that is passed into timestring can not be parsed then an error will be thrown:

const timestring = require('@navy.gif/timestring')

let str = 'aaabbbccc'
let time = timestring(str) // will throw an error