Go to file
2016-07-30 18:03:48 +03:00
.codeclimate.yml back to basics 2016-01-15 10:02:28 +00:00
.editorconfig update project meta 2015-04-30 10:30:41 +01:00
.gitignore refactor(coverage): refactor code coverage generation 2016-03-06 21:49:16 +00:00
.jscsrc remove redundant cs rule 2016-01-15 11:26:11 +00:00
.npmignore back to basics 2016-01-15 10:02:28 +00:00
.travis.yml refactor(coverage): refactor code coverage generation 2016-03-06 21:49:16 +00:00
CHANGELOG.md refactor internals 2016-01-15 20:13:52 +00:00
CONTRIBUTING.md docs(contributing): update contributing info 2016-03-06 21:49:29 +00:00
index.js added ms unit 2016-07-30 17:56:51 +03:00
LICENSE add license 2014-07-18 20:28:23 +01:00
package.json chore(package): update watch to version 0.19.1 2016-06-20 19:54:12 +01:00
README.md added ms docs 2016-07-30 18:03:48 +03:00
test.js added ms unit 2016-07-30 17:56:51 +03:00

Timestring

Version Build Status Code Climate Coveralls npm License Commitizen friendly

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

Installation

npm install --save timestring

Usage

Overview

var timestring = require('timestring');

var str = '1h 15m';
var 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:

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

console.log(time); // will log 98718

and can be as messy as you like:

var str = '1 d    3HOurS 25              min         1   8s';
var 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. mth, mths, month, months - will parse to months
  8. y, yr, yrs, year, years - will parse to years

Keywords can be used interchangeably:

var str = '1day 15h 20minutes 15s';
var 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
var str = '22h 16m';

var hours = timestring(str, 'h'); // 22.266666666666666
var days = timestring(str, 'd'); // 0.9277777777777778
var weeks = timestring(str, 'w'); // 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

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
var str = '1d';
var opts = {
	hoursPerDay: 1
}

var 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.

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

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

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