UNPKG

express-post-task-scheduler

Version:

A lightweight npm package to create and manage scheduled tasks using Express middleware. Configure tasks via POST requests and execute them at specified times seamlessly.

37 lines (36 loc) 1.36 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.timeConvertor = timeConvertor; const dayjs_1 = __importDefault(require("dayjs")); /** * To validate and convert a date string into various formats * @param executeTime for example: "2024-12-30 17:00:00" * @param compareCurrentTime If it's true, if the executeTime before current time will make returned field isValid as false */ function timeConvertor(executeTime, compareCurrentTime = true) { const invalidReturnObject = { isValid: false, error: "", dayjsObject: undefined, formattedTime: undefined, }; try { const _executeTime = (0, dayjs_1.default)(executeTime); if (compareCurrentTime && _executeTime.isBefore((0, dayjs_1.default)())) { invalidReturnObject.error = "Execute time is before current time"; return invalidReturnObject; } return { isValid: true, dayjsObject: _executeTime, formattedTime: _executeTime.format("YYYY-MM-DD HH:mm:ss"), }; } catch { invalidReturnObject.error = `Incorrect time format: ${executeTime}`; return invalidReturnObject; } }