0.1.0 • Published 7 years ago

time-funcs v0.1.0

Weekly downloads
50
License
MIT
Repository
github
Last release
7 years ago

time-funcs

A very limited subset of time functions I use every day

Install

npm i time-funcs

Package on npm

API

days(data)

Convert a data time object to a duration in days

const days = require('time-funcs/days')

// 2
days({hours:48})

// 0.5
days({hours:12})

// 0.041666666666666664
days({hours:1})

// 0.041666666666666664
days({minutes:60})

// 0.999999988425926
days({hours:23, minutes:59, seconds:59, milliseconds:999})

hours(data)

Convert a data time object to a duration in hours

const hours = require('time-funcs/hours')

// 24
hours({days:1})

// 2
hours({minutes:120})

// 0.5
hours({minutes:30})

// 0.9999997222222222
hours({minutes:59, seconds:59, milliseconds:999})

minutes(data)

Convert a data time object to a duration in minutes

const minutes = require('time-funcs/minutes')

// 120
minutes({hours:2})

// 2
minutes({seconds:120})

// 0.5
minutes({seconds:30})

// 0.9999833333333333
minutes({seconds:59, milliseconds:999})

seconds(data)

Convert a data time object to a duration in seconds

const seconds = require('time-funcs/seconds')

// 120
seconds({minutes:2})

// 1
seconds({milliseconds:1000})

// 0.5
seconds({seconds:30})

// 60.999
seconds({minutes:1, milliseconds:999})

milliseconds(data)

Convert a data time object to a duration in milliseconds

const milliseconds = require('time-funcs/milliseconds')

// 60000
milliseconds({minutes:1})

// 1000
milliseconds({seconds:1})

// 61001
milliseconds({minutes:1, seconds:1, milliseconds:1})

duration(data, unit)

Convert a duration into a time objet

ArgumentAction
datathe duration
unitoptional unit, default to ms

Valid unit values

UnitAction
msdata as milliseconds
sdata as seconds
mdata as minutes
hdata as hours
ddata as days
const duration = require('time-funcs/duration')

// {seconds: 2}
duration(2000, 'ms')

// {minutes: 1}
duration(60000, 'ms')

// {seconds: 59, milliseconds: 900}
duration(59.9, 's')

// {minutes: 1, milliseconds: 1}
duration(60.001, 's')

// {hours: 1}
duration(3600, 's')

// {days: 1}
duration(86400, 's')

// {hours: 1, minutes: 6}
duration(1.1, 'h')

timestamp(pattern, date)

Timestamp formated by a string pattern and some special chars

ArgumentAction
patternthe string pattern, default to H:M:S.L
dateoptional date, default to now

Pattern special chars

CharAction
Ythe year with the century
ythe year without the century (00-99)
zthe year without the century, padded with a leading space for single digit values (1-9)
Othe month, padded to 2 digits (01-12)
othe month
pthe month, padded with a leading space for single digit values (1-9)
Dday of the month, padded to 2 digits (01-31)
dday of the month
eday of the month, padded with a leading space for single digit values (1-9)
Hthe hour (24-hour clock), padded to 2 digits (00-23)
hthe hour (24-hour clock)
ithe hour (24-hour clock), padded with a leading space for single digit values (0-9)
Mthe minute, padded to 2 digits (00-59)
mthe minute
nthe minute, padded with a leading space for single digit values (0-9)
Sthe second, padded to 2 digits (00-60)
sthe second
tthe second, padded with a leading space for single digit values (0-9)
Lthe milliseconds, padded to 3 digits
lthe milliseconds (only the first digit)
const timestamp = require('time-funcs/timestamp')

// 09 nov 2014 09:27:01.002
var d = new Date(2014, 10, 9, 9, 27, 1, 20)

// [2014-11-09]
timestamp('[Y-O-D]', d)

// 09/11/2014
timestamp('D/O/Y', d)

// 09:27:01.020
timestamp(null, d)

durastamp(seconds, decimal, pad)

Convert a duration in seconds in a formated like a video player timestamp

ArgumentAction
secondsthe duration to format
decimaloptional decimal count of milliseconds, default to 0. 0 to 3 accepted
padoptional 0 padding for leading hours of minutes, default to false
const durastamp = require('time-funcs/durastamp')

// 0:01
durastamp(1)

// 1:01
durastamp(61)

// 10:00
durastamp(600)

// 1:00:00
durastamp(3600)

// 1:00.2
durastamp(60.234, 1)

// 1:00.234
durastamp(60.234, 3)

// 01:01
durastamp(61, 0, true)

License

MIT