UNPKG

khmer-date-utils

Version:

This package is used to convert date to khmer date, date range, date utils

38 lines 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToKhmerDate = ToKhmerDate; const khmer_number_1 = require("./khmer-number"); function ToKhmerDate(date = new Date(), format = 'dd/MM/yyyy') { // Extract day, month, and year from the Date object const day = (0, khmer_number_1.ToKhmerNumber)(date.getDate()); const month = date.getMonth() + 1; // Months are zero-indexed const year = date.getFullYear(); const shortYear = (0, khmer_number_1.ToKhmerNumber)(year.toString().slice(-2)); // Last two digits of the year // Define month names in Khmer const monthNames = [ 'មករា', 'កុម្ភៈ', 'មីនា', 'មេសា', 'ឧសភា', 'មិថុនា', 'កក្កដា', 'សីហា', 'កញ្ញា', 'តុលា', 'វិច្ឆិកា', 'ធ្នូ' ]; // Get month name or number based on the format const getMonth = (format) => { if (format.includes('MMMM')) { return monthNames[month - 1]; // Full month name } else if (format.includes('MMM')) { return monthNames[month - 1].substring(0, 3); // Short month name (first 3 characters) } else { return (0, khmer_number_1.ToKhmerNumber)(month); // Month number } }; // Replace placeholders in the format string const formattedDate = format .replace('yyyy', (0, khmer_number_1.ToKhmerNumber)(year)) // Replace full year first .replace('yy', shortYear) // Replace short year .replace('MMMM', getMonth(format)) // Replace full month name .replace('MMM', getMonth(format)) // Replace short month name .replace('MM', getMonth(format)) // Replace month number .replace('dd', day); // Replace day return formattedDate; } //# sourceMappingURL=khmer-date.js.map