add timestampmsOrNA entry

This commit is contained in:
Matthias 2023-09-06 20:29:30 +02:00
parent 1ba38a38e1
commit b8013367fe
2 changed files with 20 additions and 0 deletions

View File

@ -38,6 +38,15 @@ export function timestampms(ts: number | Date): string {
return formatDate(toDate(ts), 'yyyy-MM-dd HH:mm:ss'); return formatDate(toDate(ts), 'yyyy-MM-dd HH:mm:ss');
} }
/**
* Convert a timestamp / Date object to String.
* Returns 'N/A' if ts is null
* @param ts Timestamp as number or date (in utc!!)
*/
export function timestampmsOrNa(ts: number | Date | null): string {
return ts ? formatDate(toDate(ts), 'yyyy-MM-dd HH:mm:ss') : 'N/A';
}
/** /**
* Convert a timestamp / Date object to String * Convert a timestamp / Date object to String
* @param ts Timestamp as number or date (in utc!!) * @param ts Timestamp as number or date (in utc!!)

View File

@ -8,6 +8,7 @@ import {
dateStringToTimeRange, dateStringToTimeRange,
timestampHour, timestampHour,
dateFromString, dateFromString,
timestampmsOrNa,
} from '@/shared/formatters'; } from '@/shared/formatters';
const { getTimeZone } = exportForTesting; const { getTimeZone } = exportForTesting;
@ -38,6 +39,16 @@ describe('timeformatter.ts', () => {
setTimezone('CET'); setTimezone('CET');
expect(timestampms(1651057500000)).toEqual('2022-04-27 13:05:00'); expect(timestampms(1651057500000)).toEqual('2022-04-27 13:05:00');
}); });
it('timestampmsOrNA convert correctly', () => {
setTimezone('UTC');
expect(timestampmsOrNa(1651057500000)).toEqual('2022-04-27 11:05:00');
setTimezone('CET');
expect(timestampmsOrNa(1651057500000)).toEqual('2022-04-27 13:05:00');
expect(timestampmsOrNa(0)).toEqual('N/A');
expect(timestampmsOrNa(null)).toEqual('N/A');
});
it('timestampToDateString converts to date', () => { it('timestampToDateString converts to date', () => {
expect(timestampToDateString(1651057500000)).toEqual('2022-04-27'); expect(timestampToDateString(1651057500000)).toEqual('2022-04-27');