Skip to main content
@spa-tools/utilities

normalizeDate()

The normalizeDate() function normalizes a given date object, timestamp integer, or date string to a native Date with provided precision.

Usage

import { normalizeDate } from '@spa-tools/utilities';

const date1 = normalizeDate(1612137600000);
console.log(date1); // 2021-02-01T00:00:00.000Z

const date2 = normalizeDate('2021-01-01T23:30:08.777Z', 'second');
console.log(date2); // 2021-01-01T23:30:08.000Z

const date3 = normalizeDate(new Date('2021-01-01T23:30:08.000Z'), 'minute');
console.log(date3); // 2021-01-01T23:30:00.000Z

const date4 = normalizeDate('2021-01-01T23:30:08.000Z', 'hour');
console.log(date4); // 2021-01-01T23:00:00.000Z

Arguments

NameTypeRequired?DefaultDescription
datestring | number | Dateyes-The date value to normalize
precision'hour' | 'minute' | 'second' | 'millisecond'no'millisecond'The smallest precision to use when normalizing the date

Returns

Date - The normalized date object.