#timestring
Attempts to parse a human readable time string into a time based value.
##Overview
var str = '1h 15m';
var time = str.parseTime();
console.log(time);
In the example above str
is just a plain old String
object. A new method to the String
objects prototype named parseTime
. This method parses the string and returns a time based value.
By default the returned time value will be in seconds.
The time string can contain as many time groups as needed:
var str = '1d 3h 25m 18s';
var time = str.parseTime();
console.log(time);
and can be as messy as you like:
var str = '1 d 3HOurS 25 min 1 8s';
var time = str.parseTime();
console.log(time);
As well as using the String
objects parseTime
method you can create a Timestring
object and parse the string manually:
var str = '1h 15m';
var time = (new Timestring()).parse(str);
console.log(time);
##Keywords
timestring will parse the following keywords into time values:
s, 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 weeksmth, mths, month, months
- will parse to monthsy, yr, yrs, year, years
- will parse to years
Keywords can be used interchangeably:
var str = '1day 15h 20minutes 15s';
var time = str.parseTime();
console.log(time);
##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 String.parseTime
or Timestring.parse
:
s
- Secondsm
- Minutesh
- Hoursd
- Daysw
- Weeksmth
- Monthsy
- Years
var str = '22h 16m';
var hours = str.parseTime('h');
var days = str.parseTime('d');
var weeks = str.parseTime('w');
var hours = (new Timestring()).parse(str, 'h');
var days = (new Timestring()).parse(str, 'd');
var weeks = (new Timestring()).parse(str, 'w');
##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
These settings can be changed by passing a settings object as an argument to String.parseTime
or to the Timestring
objects constructor.
The following settings are configurable:
hoursPerDay
daysPerWeek
weeksPerMonth
monthsPerYear
var str = '1d';
var settings = {
hoursPerDay: 1
}
var time = str.parseTime('h', settings);
var time = (new Timestring(settings)).parse(str, 'h');
console.log(time);
In the example of 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 deafult 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 settings = {
hoursPerDay: 7.5,
daysPerWeek: 5
}
var today = document.querySelector('time-input').value,
thisWeek = document.querySelector('time-input').value;
var hoursToday = today.parseTime('h', settings),
daysThisWeek = thisWeek.parseTime('d', settings);
var hoursToday = (new Timestring(settings)).parse(today, 'h'),
daysThisWeek = (new Timestring(settings)).parse(thisWeek, 'd');
console.log(hoursToday);
console.log(daysThisWeek);
##Installation
Browser
All you need to do to get timestring working in the browser is download / clone this repo and make sure you include the timestring.js
script on your page:
<script src="<path-to-src>/timestring.js"></script>
Node.js
Timestring is also node compatible. To install for a project, navigate to the projects root folder and in your terminal and type the following:
npm install timestring
You may need to use sudo
if you get errors.
In your node application you need to require the timestirng module:
var Timestring = require('timestring');
Once you have done this, you will beable to use timestring in node, the same way you do in the browser!