@kdominic/time-span

1.0.0 • Public • Published

TimeSpan

Functions such as setTimeout / setInterval and similar functions always expect the time in milliseconds and to avoid having to convert 5 minutes into 5 * 60 * 1000 each time, this class was created.

Example

const TimeSpan = require('@kdominic/time-span');

console.log(TimeSpan.parse('42min'));               //  2520000 ms
console.log(TimeSpan.parse('1d 2h 3min 4s 5ms'));   // 93784005 ms

Another example

Delete all files older than 7 days:

const path      = require('node:path');
const fs        = require('node:fs');

const TimeSpan  = require('@kdominic/time-span');

const cleanPath = 'pathToClean';

if (fs.existsSync(cleanPath)) {
    fs.readdirSync(cleanPath).forEach((file) => {
        const filePath = path.join(cleanPath, file)
        const stat     = fs.statSync(filePath);
    
        if (stat.isFile()) {
            try {
                if (Date.now() > new TimeSpan(new Date(stat.ctime)).add('7d')) {
                    fs.rmSync(filePath);
                }
            }
            catch (_) {}
        } 
    });
}

Readme

Keywords

none

Package Sidebar

Install

npm i @kdominic/time-span

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

7.83 kB

Total Files

4

Last publish

Collaborators

  • kdominic