Parse a human readable time string into a time based value
Attempts to parse a human readable time string into a time based value.
var str = '1h 15m';var time = strparseTime;console.logtime; // will log 4500
In the example above
str is just a plain old
String object. A new method is added 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 = strparseTime;console.logtime; // will log 98718
and can be as messy as you like:
var str = '1 d 3HOurS 25 min 1 8s';var time = strparseTime;console.logtime; // will log 98718
As well as using the
parseTime method you can create a
Timestring object and parse the string manually:
var str = '1h 15m';var time = parsestr;console.logtime; // will log 4500
Timestring will parse the following keywords into time values:
s, sec, secs, second, seconds- will parse to seconds
m, min, mins, minute, minutes- will parse to minutes
h, hr, hrs, hour, hours- will parse to hours
d, day, days- will parse to days
w, week, weeks- will parse to weeks
mth, mths, month, months- will parse to months
y, yr, yrs, year, years- will parse to years
Keywords can be used interchangeably:
var str = '1day 15h 20minutes 15s';var time = strparseTime;console.logtime; // 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
var str = '22h 16m';var hours = strparseTime'h'; // 22.266666666666666var days = strparseTime'd'; // 0.9277777777777778var weeks = strparseTime'w'; // 0.13253968253968254// orvar hours = parsestr 'h'; // 22.266666666666666var days = parsestr 'd'; // 0.9277777777777778var weeks = parsestr 'w'; // 0.13253968253968254
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:
var str = '1d';var settings =hoursPerDay: 1var time = strparseTime'h' settings;// orvar time = settingsparsestr 'h';console.logtime; // 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 deafult there are 24 hours in a day) but because
hoursPerDay has been set to
1d will now only parse to
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.5daysPerWeek: 5// get time values from form inputvar today = documentquerySelector'time-input'value // '1d'thisWeek = documentquerySelector'time-input'value; // '1w'// parse timesvar hoursToday = todayparseTime'h' settingsdaysThisWeek = thisWeekparseTime'd' settings;// orvar hoursToday = settingsparsetoday 'h'daysThisWeek = settingsparsethisWeek 'd';console.loghoursToday; // will log 7.5console.logdaysThisWeek; // will log 5
All you need to do to get timestring working in the browser is download / clone this repo and make sure you include the
dist/timestring.min.js script on your page:
Alternatively you can you use bower to manage this dependency for you:
bower install timestring --save
To install for a node application, navigate to the projects root folder and in your terminal 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 timestring module:
var Timestring = require'timestring';
Once you have done this, you will be able to use timestring in your node application, the same way you do in the browser!