Support null date for tsformat with timezone

This commit is contained in:
Matthias 2023-09-07 06:45:18 +02:00
parent 9c712cca18
commit bbc99b2164
2 changed files with 6 additions and 1 deletions

View File

@ -53,7 +53,10 @@ export function timestampmsOrNa(ts: number | Date | null): string {
* @param timezone timezone to use * @param timezone timezone to use
* @returns formatted date in desired timezone (or globally configured timezone) * @returns formatted date in desired timezone (or globally configured timezone)
*/ */
export function timestampmsWithTimezone(ts: number | Date, timezone?: string): string { export function timestampmsWithTimezone(ts: number | Date | null, timezone?: string): string {
if (!ts) {
return 'N/A';
}
return formatDate(toDate(ts), 'yyyy-MM-dd HH:mm:ss (z)', timezone); return formatDate(toDate(ts), 'yyyy-MM-dd HH:mm:ss (z)', timezone);
} }

View File

@ -32,6 +32,8 @@ describe('timeformatter.ts', () => {
expect(timestampmsWithTimezone(1651057500000)).toEqual('2022-04-27 11:05:00 (UTC)'); expect(timestampmsWithTimezone(1651057500000)).toEqual('2022-04-27 11:05:00 (UTC)');
setTimezone('UTC'); setTimezone('UTC');
expect(timestampmsWithTimezone(1651057500000)).toEqual('2022-04-27 11:05:00 (UTC)'); expect(timestampmsWithTimezone(1651057500000)).toEqual('2022-04-27 11:05:00 (UTC)');
expect(timestampmsWithTimezone(0)).toEqual('N/A');
expect(timestampmsWithTimezone(null)).toEqual('N/A');
}); });
it('timestampms convert correctly', () => { it('timestampms convert correctly', () => {
setTimezone('UTC'); setTimezone('UTC');