Node.js
Installation
Section titled “Installation”npm install date-wizWorks with both require() (CommonJS) and import (ESM).
ESM (recommended)
Section titled “ESM (recommended)”// script.mjs — or any .js file with "type": "module" in package.jsonimport { format, addBusinessDays, getRelativeTime } from 'date-wiz';
console.log(format(new Date(), 'DD MMM YYYY'));// → "15 Mar 2026"CommonJS
Section titled “CommonJS”const { format, addBusinessDays, getRelativeTime } = require('date-wiz');
console.log(format(new Date(), 'DD MMM YYYY'));// → "15 Mar 2026"Express API — formatted timestamps
Section titled “Express API — formatted timestamps”import express from 'express';import { smartFormat, getRelativeTime } from 'date-wiz';
const app = express();
app.get('/posts', async (req, res) => { const locale = req.headers['accept-language']?.split(',')[0] ?? 'en';
const posts = await db.getPosts();
res.json(posts.map(post => ({ ...post, displayDate: smartFormat(post.createdAt, { locale }), relativeDate: getRelativeTime(post.createdAt, { locale }), })));});SLA report script
Section titled “SLA report script”import { addBusinessDays, countBusinessDays, format, checkIsBusinessDay } from 'date-wiz';
const PUBLIC_HOLIDAYS = [ '2026-01-01', // New Year '2026-03-26', // Independence Day (Bangladesh) '2026-12-25', // Christmas];
function generateSLAReport(tickets: Array<{ id: string; opened: Date }>) { return tickets.map(ticket => { const deadline = addBusinessDays(ticket.opened, 5, { holidays: PUBLIC_HOLIDAYS, }); const businessDaysLeft = countBusinessDays(new Date(), deadline, { holidays: PUBLIC_HOLIDAYS, });
return { id: ticket.id, opened: format(ticket.opened, 'YYYY-MM-DD'), deadline: format(deadline, 'YYYY-MM-DD'), businessDaysLeft, isBreached: businessDaysLeft < 0, }; });}Cron job — only run on business days
Section titled “Cron job — only run on business days”import cron from 'node-cron';import { checkIsBusinessDay, isWithinWorkingHours } from 'date-wiz';
// Run every 30 minutes, but only process during business hourscron.schedule('*/30 * * * *', () => { const now = new Date();
if (!checkIsBusinessDay(now)) { console.log('Weekend — skipping'); return; }
if (!isWithinWorkingHours(now, { start: '09:00', end: '18:00' })) { console.log('Outside working hours — skipping'); return; }
processQueue();});