UNPKG

@nodescript/stdlib

Version:
65 lines (64 loc) 2.17 kB
import { parseDate } from '../lib/date.js'; export const module = { version: '1.1.3', moduleName: 'Date / Parse', description: 'Returns individual components of the date, i.e. year, month, week, day of month, etc.', params: { date: { schema: { type: 'any' }, attributes: { valuePlaceholder: 'now', }, }, }, result: { schema: { type: 'any' }, } }; export const compute = params => { const date = parseDate(params.date); return { local: { milliseconds: date.getMilliseconds(), seconds: date.getSeconds(), minutes: date.getMinutes(), hours: date.getHours(), dayOfMonth: date.getDate(), dayOfWeek: date.getDay(), month: date.getMonth() + 1, year: date.getFullYear(), date: formatDate(date.getFullYear(), date.getMonth() + 1, date.getDate()), time: formatTime(date.getHours(), date.getMinutes(), date.getSeconds()), }, utc: { milliseconds: date.getUTCMilliseconds(), seconds: date.getUTCSeconds(), minutes: date.getUTCMinutes(), hours: date.getUTCHours(), dayOfMonth: date.getUTCDate(), dayOfWeek: date.getUTCDay(), month: date.getUTCMonth() + 1, year: date.getUTCFullYear(), date: formatDate(date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate()), time: formatTime(date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()), }, iso: date.toISOString(), full: date.toUTCString(), epoch: date.getTime(), tzOffset: date.getTimezoneOffset(), }; }; function formatDate(year, month, day) { return [ String(year).padStart(4, '0'), String(month).padStart(2, '0'), String(day).padStart(2, '0'), ].join('-'); } function formatTime(hours, minutes, seconds) { return [ String(hours).padStart(2, '0'), String(minutes).padStart(2, '0'), String(seconds).padStart(2, '0'), ].join(':'); }