timestringconverter
v2.2.5
Published
generetes a number from a time string
Downloads
52
Readme
Timestringconverter
An easy way to use readable timestrings in your code.
Table of Contents
About
This library is written because of the problem with jsonwebtokens and cookies. I find it a lot easier to say 5d instead of calculating the amount of ms in 5 days. This library is also very useful for other things like timeouts and intervals.
Features
- Convert timestrings to milliseconds
- Convert timestrings to seconds
- Convert timestrings to minutes
- Convert timestrings to hours
- Convert timestrings to days
- Works in ESM CJS and typescript
Installation
To install this library, you can use npm or yarn.
npm install timestringconverteryarn add timestringconverterUsage
important: you need to use d, h, m, s and ms to define the time. You can also use uppercase letters.
const timestringconverter = require('timestringconverter');
console.log(timestringconverter.ToMs('5d')); // 432000000
console.log(timestringconverter.ToSec('5d2h4m')); // 439440
console.log(timestringconverter.ToMin('5d9h')); // 7740
console.log(timestringconverter.ToHour('5d2H')); // 122
console.log(timestringconverter.ToDay('2h500m10000ms')); // 0.43067129629629625Input validation
- Input must be a non-empty string.
- Supported units are:
d,h,m,s,ms. - Invalid formats throw an error.
try {
timestringconverter.ToSec('');
} catch (error) {
console.error(error.message);
// Input is required and must be a non-empty string.
}
try {
timestringconverter.ToSec('abc');
} catch (error) {
console.error(error.message);
// Invalid input. Use values like 1d2h30m15s250ms.
}Contributing
Guidelines for contributing to the project. Include information on how to report issues, suggest improvements, or submit pull requests.
License
This project is licensed under the MIT License - see the LICENSE file for details.
