time-len
v1.0.6
Published
Use a string to describe a time length
Downloads
8
Maintainers
Readme
Install
$ npm i time-len
OR
$ yarn add time-len
Examples
var timeLen = require('time-len');
const SECOND = 1000;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
const WEEK = 7 * DAY;
console.assert(timeLen('1ms') === 1, '1ms'); // 1毫秒
console.assert(timeLen('1s') === SECOND, '1s'); // 1秒
console.assert(timeLen('1m') === MINUTE, '1m'); // 1分钟
console.assert(timeLen('1h') === HOUR, '1h');// 1小时
console.assert(timeLen('1d') === DAY, '1d');// 1天
console.assert(timeLen('1w') === WEEK, '11');// 1周
console.assert(timeLen('1w1d1h1m1s1ms') === (WEEK + DAY + HOUR + MINUTE + SECOND + 1), '1w1d1h1m1s1ms');
console.assert(timeLen('.5h') === (30 * MINUTE), '.5h'); // 半小时
console.assert(timeLen('1.5h') === (90 * MINUTE), '.5h'); // 一个半小时
console.assert(timeLen('.5d') === (12 * HOUR), '.5d'); // 半天
console.assert(timeLen('1d25h') === (2 * DAY + HOUR), '1d25h'); // 一天零25小时(两天零1小时)
console.assert(timeLen('66m') === (HOUR + 6 * MINUTE), '66m'); // 66分钟(1小时零6分钟)
console.assert(timeLen(HOUR + 6 * MINUTE) === '1h6m', '1h6m'); // 66分钟(1小时零6分钟)
console.assert(timeLen(3 * WEEK) === '3w', '3w'); // 3周