[Edit]
+
0
-
0

JavaScript / Node.js - format date and time (custom implementation of render as string function)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
// ------------------------------------------------------------------------- // index.js // ------------------------------------------------------------------------- const {formatDate} = require('./dateUtils.js'); // Usage example 1: { // <-- DATE --> <--- TIME ----> const date = new Date(2023, 10, 10, 15, 30, 45, 925); // 2023-11-10 15:30:45.925 console.log(formatDate(date, 'YYYY-MM-dd HH:mm:ss')); // 2023-11-10 15:30:45 console.log(formatDate(date, 'YYYY-MM-dd')); // 2023-11-10 console.log(formatDate(date, 'HH:mm:ss')); // 15:30:45 console.log(formatDate(date, 'DD, MMM YYYY')); // Fri, Nov 2023 console.log(formatDate(date, 'h:mm a')); // 3:30 PM console.log(formatDate(date, 'hh:mm a')); // 03:30 PM } // Usage example 2: { // <-- DATE --> <--- TIME ----> const date = new Date(2023, 10, 10, 15, 30, 45, 925); // 2023-11-10 15:30:45.925 console.log(formatDate()); console.log(formatDate(date)); // 2023-11-10 15:30:45 } // See also: // // 1. https://dirask.com/snippets/JavaScript-format-date-and-time-custom-implementation-of-render-as-string-function-jmLXLp // 2. https://dirask.com/snippets/JavaScript-format-date-and-time-custom-implementation-of-render-as-string-function-1ek35p // ------------------------------------------------------------------------- // dateUtils.js // ------------------------------------------------------------------------- const MONTH_NAMES = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; const DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; const MONTH_SHORTCUTS = MONTH_NAMES.map((name) => name.substring(0, 3)); const DAY_SHORTCUTS = DAY_NAMES.map((name) => name.substring(0, 3)); const cutString = (string, length) => { return string.substring(0, length); }; const padString = (string, length) => { for (let i = string.length; i < length; ++i) { string = '0' + string; } return string; }; const renderNumber$1 = (number, length) => { const string = String(number); return padString(string, length); }; const renderNumber$2 = (number, length) => { let string = String(number); if (string.length > length) { return cutString(string, length); } return padString(string, length); }; const COMPONENT_PATTERN = /(a|ss?|mm?|hh?|HH?|dd?|DD?|MM?M?M?|yy(?:yy)?|YY(?:YY)?)/g; const COMPONENT_ACTIONS = { 'a': (date) => { // a AM or PM marker const hours = date.getHours(); return hours < 12 ? 'AM' : 'PM'; }, 's': (date) => renderNumber$1(date.getSeconds(), 1), // s Second in the minute ( 0-59) 'ss': (date) => renderNumber$1(date.getSeconds(), 2), // ss Second in the minute (00-59) 'm': (date) => renderNumber$1(date.getMinutes(), 1), // m Minute in the hour ( 0-59) 'mm': (date) => renderNumber$1(date.getMinutes(), 2), // mm Minute in the hour (00-59) 'h': (date) => { // h Hour in the day in AM/PM ( 1-12) const hours = date.getHours(); if (hours === 0 || hours === 12) { return '12'; } return renderNumber$1(hours % 12, 1) }, 'hh': (date) => { // hh Hour in the day in AM/PM (01-12) const hours = date.getHours(); if (hours === 0 || hours === 12) { return '12'; } return renderNumber$1(hours % 12, 2) }, 'H': (date) => renderNumber$1(date.getHours(), 1), // H Hour in the day ( 0-23) 'HH': (date) => renderNumber$1(date.getHours(), 2), // HH Hour in the day (00-23) 'd': (date) => renderNumber$1(date.getDate(), 1), // d Day number of the month ( 1-31 depending on month) 'dd': (date) => renderNumber$1(date.getDate(), 2), // dd Day number of the month (01-31 depending on month) 'D': (date) => DAY_NAMES[date.getDay()], // D Full day name 'DD': (date) => DAY_SHORTCUTS[date.getDay()], // DD Short day name 'M': (date) => renderNumber$1(date.getMonth() + 1, 1), // M Month number in the year ( 1-12) 'MM': (date) => renderNumber$1(date.getMonth() + 1, 2), // MM Month number in the year (01-12) 'MMM': (date) => MONTH_SHORTCUTS[date.getDate()], // MMM Short month name 'MMMM': (date) => MONTH_NAMES[date.getDate()], // MMMM Full month name 'yy': (date) => renderNumber$2(date.getFullYear(), 2), // yy Year (00-99) 'yyyy': (date) => renderNumber$1(date.getFullYear(), 4), // yyyy Year (0000-9999) 'YY': (date) => renderNumber$2(date.getFullYear(), 2), // yy Year (00-99) 'YYYY': (date) => renderNumber$1(date.getFullYear(), 4) // yyyy Year (0000-9999) }; const formatDate = (date = new Date(), format = 'YYYY-MM-dd HH:mm:ss') => { return format.replace(COMPONENT_PATTERN, (match, group) => { const action = COMPONENT_ACTIONS[group]; if (action) { return action(date); } return group; }); }; module.exports = { MONTH_NAMES, DAY_NAMES, MONTH_SHORTCUTS, DAY_SHORTCUTS, formatDate };