9386e496d0
Bump minimist from 1.2.5 to 1.2.6 |
||
---|---|---|
.github | ||
.editorconfig | ||
.gitignore | ||
.npmignore | ||
CHANGELOG.md | ||
CONTRIBUTING.md | ||
index.js | ||
LICENSE | ||
package-lock.json | ||
package.json | ||
README.md | ||
test.js |
timestring
Parse a human readable time string into a time based value.
Installation
npm install --save timestring
Usage
Overview
const timestring = require('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('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('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:
ms, milli, millisecond, milliseconds
- will parse to millisecondss, sec, secs, second, seconds
- will parse to secondsm, min, mins, minute, minutes
- will parse to minutesh, hr, hrs, hour, hours
- will parse to hoursd, day, days
- will parse to daysw, week, weeks
- will parse to weeksmon, mth, mths, month, months
- will parse to monthsy, yr, yrs, year, years
- will parse to years
Keywords can be used interchangeably:
const timestring = require('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
:
ms
- Millisecondss
- Secondsm
- Minutesh
- Hoursd
- Daysw
- Weeksmth
- Monthsy
- Years
const timestring = require('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:
- There are 24 hours per day
- There are 7 days per week
- There are 4 weeks per month
- There are 12 months per year
- 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:
hoursPerDay
daysPerWeek
weeksPerMonth
monthsPerYear
daysPerYear
const timestring = require('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('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('timestring')
let str = 'aaabbbccc'
let time = timestring(str) // will throw an error