UNPKG

datetime-fcs

Version:

A library to format dates, track countdowns, and calculate durations.

52 lines (48 loc) 2.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatDate = exports.durationBetweenDates = exports.countdown = void 0; var _dateFns = require("date-fns"); // src/index.js /** * Countdown function: Returns remaining time until a target date. * @param {string} endTime - Target end time in ISO format. * @returns {string} Countdown in the format 'X hours, Y minutes, Z seconds'. */ var countdown = exports.countdown = function countdown(endTime) { var now = new Date(); var target = (0, _dateFns.parseISO)(endTime); var secondsRemaining = (0, _dateFns.differenceInSeconds)(target, now); if (secondsRemaining <= 0) { return "The countdown has ended!"; } var hours = Math.floor(secondsRemaining / 3600); var minutes = Math.floor(secondsRemaining % 3600 / 60); var seconds = secondsRemaining % 60; return "".concat(hours, " hours, ").concat(minutes, " minutes, ").concat(seconds, " seconds remaining"); }; /** * Format a given date to a specific format. * @param {string} date - The date string to format. * @param {string} formatString - The desired format (e.g., "yyyy-MM-dd HH:mm:ss"). * @returns {string} Formatted date. */ var formatDate = exports.formatDate = function formatDate(date, formatString) { return (0, _dateFns.format)(new Date(date), formatString); }; /** * Calculate duration between two dates. * @param {string} startDate - The start date (ISO format). * @param {string} endDate - The end date (ISO format). * @returns {string} Duration in the format 'X hours, Y minutes, Z seconds'. */ var durationBetweenDates = exports.durationBetweenDates = function durationBetweenDates(startDate, endDate) { var start = (0, _dateFns.parseISO)(startDate); var end = (0, _dateFns.parseISO)(endDate); var durationInSeconds = (0, _dateFns.differenceInSeconds)(end, start); var hours = Math.floor(durationInSeconds / 3600); var minutes = Math.floor(durationInSeconds % 3600 / 60); var seconds = durationInSeconds % 60; return "".concat(hours, " hours, ").concat(minutes, " minutes, ").concat(seconds, " seconds"); };