UNPKG

@nodescript/stdlib

Version:
50 lines (49 loc) 1.44 kB
import { parseDate } from '../lib/date.js'; export const module = { version: '1.0.5', moduleName: 'Date / Difference', description: 'Computes a difference between two dates in specified units.', params: { fromDate: { schema: { type: 'any' }, attributes: { valuePlaceholder: 'now', }, }, toDate: { schema: { type: 'any' }, attributes: { valuePlaceholder: 'now', }, }, unit: { schema: { type: 'string', enum: ['day', 'hour', 'minute', 'second', 'millisecond'], default: 'day', } } }, result: { schema: { type: 'any' }, } }; export const compute = params => { const { fromDate, toDate, unit } = params; const from = parseDate(fromDate); const to = parseDate(toDate); const ms = to.getTime() - from.getTime(); switch (unit) { case 'millisecond': return round(ms); case 'second': return round(ms / 1000); case 'minute': return round(ms / 60 / 1000); case 'hour': return round(ms / 60 / 60 / 1000); case 'day': return round(ms / 24 / 60 / 60 / 1000); default: { throw new Error(`Unsupported unit: ${unit}`); } } }; function round(ms) { return Math.sign(ms) * Math.floor(Math.abs(ms)); }