Convert string to milliseconds
Supports single expressions like "1s" and complex ones "1m 1s"
npm install stringtoms
<script src="https://cdn.jsdelivr.net/npm/stringtoms/standalone/stringtoms.js"></script>
or
<script src="https://cdn.jsdelivr.net/npm/stringtoms/standalone/stringtoms.min.js"></script>
import { stringtoms } from 'stringtoms';
const stringtoms = require('stringtoms');
<script src="https://cdn.jsdelivr.net/npm/stringtoms/standalone/stringtoms.min.js"></script>
<script>
// global stringtoms variable exists
</script>
- milliseconds as 'ms', 'mil', 'milli', 'millisecond', 'milliseconds'
- seconds as 's', 'se', 'sec', 'second', 'seconds'
- minutes as 'm', 'mi', 'min', 'minute', 'minutes'
- hours as 'h', 'ho', 'hour', 'hours'
1h, 1m, 1s and so on...
import { stringtoms } from 'stringtoms';
stringtoms('1ms');
stringtoms('1s');
stringtoms('1m');
stringtoms('1h');
1h 1m, 1m 1s and so on...
import { stringtoms } from 'stringtoms';
stringtoms('1s 1ms'); // means 1 second and 1 millisecond
stringtoms('1m 1s'); // means 1 minute and 1 second
stringtoms('1m 1m'); // means 1 minute and 1 minute
stringtoms('1h 1ms'); // means 1 hour and 1 millisecond
1h is equal to 1 h is equal to 1 hour
Any \W symbols are removed. Spaces are not needed.
import { stringtoms } from 'stringtoms';
stringtoms('1h') === stringtoms('1 h') === stringtoms('1 hour') === stringtoms('1#hour');
stringtoms('1m1s') === stringtoms('1 m 1 s') === stringtoms('1 m1 s') === stringtoms('1m 1s');
1h 1 is 1h 1m, 1 1m is 1h 1m.
Tries to complete left and right for missing units.
import { stringtoms } from 'stringtoms';
stringtoms('1h 1') === stringtoms('1 h 1m') === stringtoms('1 1m');
stringtoms('1 1s 1') === stringtoms('1 m 1 s 1 ms') === stringtoms('1m 1s 1ms');