Better date format
This commit is contained in:
@ -1,9 +1,10 @@
|
|||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import {PE} from './dist/index.mjs';
|
import {formatDate} from './dist/index.mjs';
|
||||||
|
|
||||||
console.log('data/Ts:n', PE`${'data/Ts'}:n`.methods);
|
console.log(formatDate('YYYY-MM-DD HH:mm A z'));
|
||||||
|
console.log(formatDate('YYYY-MM-DD HH:mm A Z'));
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.26.26",
|
"version": "0.27.0",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
10
src/math.ts
10
src/math.ts
@ -48,3 +48,13 @@ export function fracToDec(frac: string) {
|
|||||||
split = (<string>split.pop()).split('/');
|
split = (<string>split.pop()).split('/');
|
||||||
return whole + (Number(split[0]) / Number(split[1]));
|
return whole + (Number(split[0]) / Number(split[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function numSuffix(num: number): string {
|
||||||
|
if (num % 100 >= 11 && num % 100 <= 13) return `${num}th`;
|
||||||
|
switch (num % 10) {
|
||||||
|
case 1: return `${num}st`;
|
||||||
|
case 2: return `${num}nd`;
|
||||||
|
case 3: return `${num}rd`;
|
||||||
|
default: return `${num}th`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
187
src/time.ts
187
src/time.ts
@ -20,6 +20,16 @@ export function adjustedInterval(cb: Function, ms: number) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function dayOfWeek(num: number): string {
|
||||||
|
return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][num] || 'Unknown';
|
||||||
|
}
|
||||||
|
|
||||||
|
export function dayOfYear(date: Date): number {
|
||||||
|
const start = new Date(`${date.getFullYear()}-01-01T00:00:00Z`);
|
||||||
|
return Math.ceil((date.getTime() - start.getTime()) / (1000 * 60 * 60 * 24));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format date
|
* Format date
|
||||||
*
|
*
|
||||||
@ -28,112 +38,91 @@ export function adjustedInterval(cb: Function, ms: number) {
|
|||||||
* @param tz Set timezone offset
|
* @param tz Set timezone offset
|
||||||
* @return {string} Formated date
|
* @return {string} Formated date
|
||||||
*/
|
*/
|
||||||
export function formatDate(format = 'YYYY-MM-DD H:mm', date: Date | number | string = new Date(), tz?: string | number): string {
|
export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | number | string = new Date(), tz: string | number = 'local'): string {
|
||||||
const timezones = [
|
if (typeof date === 'number' || typeof date === 'string') date = new Date(date);
|
||||||
['IDLW', -12],
|
if (isNaN(date.getTime())) throw new Error('Invalid date input');
|
||||||
['SST', -11],
|
const numericTz = typeof tz === 'number';
|
||||||
['HST', -10],
|
const localTz = tz === 'local' || (!numericTz && tz.toLowerCase?.() === 'local');
|
||||||
['AKST', -9],
|
const offsetMinutes = numericTz ? tz * 60 : 0;
|
||||||
['PST', -8],
|
const adjustedDate = date;
|
||||||
['MST', -7],
|
const tzName = localTz ? Intl.DateTimeFormat().resolvedOptions().timeZone : numericTz ? `UTC${tz >= 0 ? '+' : ''}${tz}` : tz;
|
||||||
['CST', -6],
|
|
||||||
['EST', -5],
|
|
||||||
['AST', -4],
|
|
||||||
['BRT', -3],
|
|
||||||
['MAT', -2],
|
|
||||||
['AZOT', -1],
|
|
||||||
['UTC', 0],
|
|
||||||
['CET', 1],
|
|
||||||
['EET', 2],
|
|
||||||
['MSK', 3],
|
|
||||||
['AST', 4],
|
|
||||||
['PKT', 5],
|
|
||||||
['IST', 5.5],
|
|
||||||
['BST', 6],
|
|
||||||
['ICT', 7],
|
|
||||||
['CST', 8],
|
|
||||||
['JST', 9],
|
|
||||||
['AEST', 10],
|
|
||||||
['SBT', 11],
|
|
||||||
['FJT', 12],
|
|
||||||
['TOT', 13],
|
|
||||||
['LINT', 14]
|
|
||||||
];
|
|
||||||
|
|
||||||
function adjustTz(date: Date, gmt: number) {
|
function getTZOffset(): string {
|
||||||
const currentOffset = date.getTimezoneOffset();
|
if (numericTz) {
|
||||||
const adjustedOffset = gmt * 60;
|
const hours = Math.floor(Math.abs(offsetMinutes) / 60);
|
||||||
return new Date(date.getTime() + (adjustedOffset + currentOffset) * 60000);
|
const minutes = Math.abs(offsetMinutes) % 60;
|
||||||
|
return `${offsetMinutes >= 0 ? '+' : '-'}${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}`;
|
||||||
|
}
|
||||||
|
if (tzName === 'UTC') return '+00:00';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const parts = new Intl.DateTimeFormat('en-US', {timeZone: tzName, timeZoneName: 'longOffset', hour: '2-digit', minute: '2-digit',}).formatToParts(adjustedDate);
|
||||||
|
const tzPart = parts.find(p => p.type === 'timeZoneName')?.value || '';
|
||||||
|
const match = tzPart.match(/([+-]\d{2}:\d{2})/);
|
||||||
|
if (match) return match[1];
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
const dtf = new Intl.DateTimeFormat('en-US', {timeZone: tzName, hour12: false, hour: '2-digit', minute: '2-digit'});
|
||||||
|
const parts = dtf.formatToParts(adjustedDate);
|
||||||
|
const targetHour = Number(parts.find(p => p.type === 'hour')?.value);
|
||||||
|
const targetMinute = Number(parts.find(p => p.type === 'minute')?.value);
|
||||||
|
const utcHour = adjustedDate.getUTCHours();
|
||||||
|
const utcMinute = adjustedDate.getUTCMinutes();
|
||||||
|
|
||||||
|
let offset = (targetHour - utcHour) * 60 + (targetMinute - utcMinute);
|
||||||
|
if (offset > 720) offset -= 1440;
|
||||||
|
if (offset < -720) offset += 1440;
|
||||||
|
|
||||||
|
const sign = offset >= 0 ? '+' : '-';
|
||||||
|
const absOffset = Math.abs(offset);
|
||||||
|
const hours = Math.floor(absOffset / 60);
|
||||||
|
const minutes = absOffset % 60;
|
||||||
|
return `${sign}${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function day(num: number): string {
|
function getTZAbbr(): string {
|
||||||
return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][num] || 'Unknown';
|
if(numericTz && tz == 0) return 'UTC';
|
||||||
}
|
try {
|
||||||
|
return new Intl.DateTimeFormat('en-US', {timeZone: tzName, timeZoneName: 'short'})
|
||||||
function doy(date: Date) {
|
.formatToParts(adjustedDate).find(p => p.type === 'timeZoneName')?.value || '';
|
||||||
const start = new Date(`${date.getFullYear()}-01-01 0:00:00`);
|
} catch {
|
||||||
return Math.ceil((date.getTime() - start.getTime()) / (1000 * 60 * 60 * 24));
|
return tzName;
|
||||||
}
|
|
||||||
|
|
||||||
function month(num: number): string {
|
|
||||||
return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][num] || 'Unknown';
|
|
||||||
}
|
|
||||||
|
|
||||||
function suffix(num: number) {
|
|
||||||
if (num % 100 >= 11 && num % 100 <= 13) return `${num}th`;
|
|
||||||
switch (num % 10) {
|
|
||||||
case 1: return `${num}st`;
|
|
||||||
case 2: return `${num}nd`;
|
|
||||||
case 3: return `${num}rd`;
|
|
||||||
default: return `${num}th`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function tzOffset(offset: number) {
|
const formatter = new Intl.DateTimeFormat('en-us', {
|
||||||
const hours = ~~(offset / 60);
|
timeZone: numericTz ? 'UTC' : tzName,
|
||||||
const minutes = offset % 60;
|
year: format.includes('YY') ? 'numeric' : undefined,
|
||||||
return (offset > 0 ? '-' : '') + `${hours}:${minutes.toString().padStart(2, '0')}`;
|
month: format.includes('MM') || format.includes('M') ? '2-digit' : undefined,
|
||||||
}
|
day: format.includes('DD') || format.includes('Do') || format.includes('D') ? '2-digit' : undefined,
|
||||||
|
hour: format.includes('HH') || format.includes('hh') || format.includes('H') || format.includes('h') ? '2-digit' : undefined,
|
||||||
|
minute: format.includes('mm') || format.includes('m') ? '2-digit' : undefined,
|
||||||
|
second: format.includes('ss') || format.includes('s') ? '2-digit' : undefined,
|
||||||
|
hourCycle: format.includes('A') || format.includes('a') ? 'h12' : 'h23',
|
||||||
|
});
|
||||||
|
|
||||||
if(typeof date == 'number' || typeof date == 'string' || date == null) date = new Date(date);
|
const parts = formatter.formatToParts(adjustedDate);
|
||||||
|
|
||||||
// Handle timezones
|
|
||||||
let t!: [string, number];
|
|
||||||
if(tz == null) tz = -(date.getTimezoneOffset() / 60);
|
|
||||||
t = <any>timezones.find(t => isNaN(<any>tz) ? t[0] == tz : t[1] == tz);
|
|
||||||
if(!t) throw new Error(`Unknown timezone: ${tz}`);
|
|
||||||
date = adjustTz(date, t[1]);
|
|
||||||
|
|
||||||
// Token mapping
|
|
||||||
const tokens: Record<string, string> = {
|
const tokens: Record<string, string> = {
|
||||||
'YYYY': date.getFullYear().toString(),
|
YYYY: adjustedDate.getFullYear().toString(),
|
||||||
'YY': date.getFullYear().toString().slice(2),
|
YY: adjustedDate.getFullYear().toString().slice(2),
|
||||||
'MMMM': month(date.getMonth()),
|
MM: parts.find(part => part.type === 'month')?.value || '',
|
||||||
'MMM': month(date.getMonth()).slice(0, 3),
|
M: (parseInt(parts.find(part => part.type === 'month')?.value || '0', 10)).toString(),
|
||||||
'MM': (date.getMonth() + 1).toString().padStart(2, '0'),
|
DD: parts.find(part => part.type === 'day')?.value || '',
|
||||||
'M': (date.getMonth() + 1).toString(),
|
D: parseInt(parts.find(part => part.type === 'day')?.value || '0', 10).toString(),
|
||||||
'DDD': doy(date).toString(),
|
HH: parts.find(part => part.type === 'hour')?.value.padStart(2, '0') || '',
|
||||||
'DD': date.getDate().toString().padStart(2, '0'),
|
H: parseInt(parts.find(part => part.type === 'hour')?.value || '0', 10).toString(),
|
||||||
'Do': suffix(date.getDate()),
|
hh: (parseInt(parts.find(part => part.type === 'hour')?.value || '0', 10) % 12 || 12).toString().padStart(2, '0'),
|
||||||
'D': date.getDate().toString(),
|
h: (parseInt(parts.find(part => part.type === 'hour')?.value || '0', 10) % 12 || 12).toString(),
|
||||||
'dddd': day(date.getDay()),
|
mm: parts.find(part => part.type === 'minute')?.value || '',
|
||||||
'ddd': day(date.getDay()).slice(0, 3),
|
m: parseInt(parts.find(part => part.type === 'minute')?.value || '0', 10).toString(),
|
||||||
'HH': date.getHours().toString().padStart(2, '0'),
|
ss: parts.find(part => part.type === 'second')?.value || '',
|
||||||
'H': date.getHours().toString(),
|
s: parseInt(parts.find(part => part.type === 'second')?.value || '0', 10).toString(),
|
||||||
'hh': (date.getHours() % 12 || 12).toString().padStart(2, '0'),
|
A: parseInt(parts.find(part => part.type === 'hour')?.value || '0', 10) >= 12 ? 'PM' : 'AM',
|
||||||
'h': (date.getHours() % 12 || 12).toString(),
|
a: parseInt(parts.find(part => part.type === 'hour')?.value || '0', 10) >= 12 ? 'pm' : 'am',
|
||||||
'mm': date.getMinutes().toString().padStart(2, '0'),
|
Z: getTZOffset(),
|
||||||
'm': date.getMinutes().toString(),
|
z: getTZAbbr(),
|
||||||
'ss': date.getSeconds().toString().padStart(2, '0'),
|
|
||||||
's': date.getSeconds().toString(),
|
|
||||||
'SSS': date.getMilliseconds().toString().padStart(3, '0'),
|
|
||||||
'A': date.getHours() >= 12 ? 'PM' : 'AM',
|
|
||||||
'a': date.getHours() >= 12 ? 'pm' : 'am',
|
|
||||||
'ZZ': tzOffset(t[1] * 60).replace(':', ''),
|
|
||||||
'Z': tzOffset(t[1] * 60),
|
|
||||||
'z': typeof tz == 'string' ? tz : (<any>t)[0]
|
|
||||||
};
|
};
|
||||||
return format.replace(/YYYY|YY|MMMM|MMM|MM|M|DDD|DD|Do|D|dddd|ddd|HH|H|hh|h|mm|m|ss|s|SSS|A|a|ZZ|Z|z/g, token => tokens[token]);
|
return format.replace(/YYYY|YY|MM|M|DD|D|HH|H|hh|h|mm|m|ss|s|A|a|Z|z/g, token => tokens[token]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -148,6 +137,10 @@ export function instantInterval(fn: () => any, interval: number) {
|
|||||||
return setInterval(fn, interval);
|
return setInterval(fn, interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function monthString(num: number): string {
|
||||||
|
return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][num] || 'Unknown';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use in conjunction with `await` to pause an async script
|
* Use in conjunction with `await` to pause an async script
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user