convert seconds to HMS (or) to hours, minutes, seconds explicitly.
npm install s2hms
contains two types of modules :
- s2hms
- s2h, s2m, s2s
takes two arguments : seconds (number
) , options (object
optional)
import { s2hms } from "s2hms";
s2hms(4342);
// 01:12:22
s2hms(1432);
// 00:23:52
s2hms(64567, { format: "short" });
// 17h:56m:07s
s2hms(432, { format: "long" });
// 07minutes:12seconds
s2hms(8573, { format: "long", separator: "__" });
// 02hours__22minutes__53seconds
key | val | type | default val |
---|---|---|---|
format | 'long', 'short', 'standard | string |
'standard' |
separator | any string char ':' , '+' , '=' , '_' ,'-' , etc... | string |
':' |
example:
with separator: '/' and format: 'short'
s2hms(983, { format: "short", separator: "/" });
// => 16m/23s
takes two arguments : seconds (number
) , options (object
optional)
import { s2h, s2m, s2s } from "s2hms";
s2h(43434);
// 12.1
s2m(9832, { format: "short" });
// 163.9 m
s2m(98907, { format: "long" });
// 1648.5 minutes
s2s(9832, { format: "short" });
// 9832 s
key | val | type | default val |
---|---|---|---|
format | 'long', 'short', 'standard' | string |
'standard' |
fallback | true, false | boolean |
false |
when fallback is set to true, conversion auto downgrades to next unit if the resulting value is less than 1 (one),
examples:
without fallback
s2h(983, { format: "short" });
// 0.3 h
with fallback set to true
s2h(983, { format: "short", fallback: true });
// 16.4 m
without fallback
s2m(46, { format: "long" });
// 0.8 minute
with fallback set to true
s2m(46, { format: "long", fallback: true });
// 46 seconds
note: fallback can be used only when a format is specified , if no format is specified and fallback is set to true , it will throw an error, this is to prevent confusion of resulting value's unit type.
s2h(634, { fallback: true });
// Error: option'fallback:true' can only be used when a format is specified
MIT