UNPKG

@yookue/ts-lang-utils

Version:

Common lang utilities for typescript

28 lines 913 B
export function formatDateTime(date, format) { if (!date || !format) { return undefined; } var entries = { 'y+': date.getFullYear(), 'M+': date.getMonth() + 1, 'd+': date.getDate(), 'h+': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, 'H+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds(), 'q+': Math.floor((date.getMonth() + 3) / 3), 'S': date.getMilliseconds() }; var result = format; for (var entry in entries) { var array = new RegExp("(".concat(entry, ")")).exec(result); if (array) { if (/(y+)/.test(entry)) { result = result.replace(array[1], entries[entry].toString().substring(4 - array[1].length)); } else { result = result.replace(array[1], array[1].length === 1 ? entries[entry].toString() : entries[entry].toString().padStart(array[1].length, '0')); } } } return result; }