A collection of date-related utilities.
- Install by executing
npm install @wojtekmaj/date-utils
oryarn add @wojtekmaj/date-utils
. - Import by adding
import * as dateUtils from '@wojtekmaj/date-utils'
. - Do stuff with it!
const now = new Date(); const startOfCentury = getCenturyStart(now);
Gets year from a given date.
import { getYear } from '@wojtekmaj/date-utils';
getYear(new Date(2019, 0, 1)); // 2019
Gets month index from a given date. For example, returns 0 for January, 1 for February and so on.
import { getMonth } from '@wojtekmaj/date-utils';
getMonth(new Date(2019, 0, 1)); // 0
Gets human-readable month number from a given date. For example, returns 1 for January, 2 for February and so on.
import { getMonthHuman } from '@wojtekmaj/date-utils';
getMonthHuman(new Date(2019, 0, 1)); // 1
Gets day of the month from a given date.
import { getDate } from '@wojtekmaj/date-utils';
getDate(new Date(2019, 0, 15)); // 15
Gets hours from a given date or string.
import { getHours } from '@wojtekmaj/date-utils';
getHours(new Date(2019, 0, 15, 22, 41, 56)); // 22
getHours('22:41'); // 22
getHours('22:41:56'); // 22
Gets minutes from a given date or string.
import { getMinutes } from '@wojtekmaj/date-utils';
getMinutes(new Date(2019, 0, 15, 22, 41, 56)); // 41
getMinutes('22:41'); // 41
getMinutes('22:41:56'); // 41
Gets seconds from a given date or string.
import { getSeconds } from '@wojtekmaj/date-utils';
getSeconds(new Date(2019, 0, 15, 22, 41, 56)); // 56
getSeconds('22:41'); // 0
getSeconds('22:41:56'); // 56
getSeconds('22:41:56.321'); // 56
Gets milliseconds from a given date or string.
import { getMilliseconds } from '@wojtekmaj/date-utils';
getMilliseconds(new Date(2019, 0, 15, 22, 41, 56, 321)); // 321
getMilliseconds('22:41'); // 0
getMilliseconds('22:41:56'); // 0
getMilliseconds('22:41:56.321'); // 321
Gets century start date from a given date.
import { getCenturyStart } from '@wojtekmaj/date-utils';
getCenturyStart(new Date(2019, 0, 1)); // new Date(2001, 0, 1)
Gets century start date from a given date.
import { getCenturyEnd } from '@wojtekmaj/date-utils';
getCenturyEnd(new Date(2019, 0, 1)); // new Date(2100, 12, 31, 23, 59, 999)
Gets previous century start date from a given date.
import { getPreviousCenturyStart } from '@wojtekmaj/date-utils';
getPreviousCenturyStart(new Date(2019, 0, 1)); // new Date(1901, 0, 1)
Gets century start date from a given date.
import { getPreviousCenturyEnd } from '@wojtekmaj/date-utils';
getPreviousCenturyEnd(new Date(2019, 0, 1)); // new Date(2000, 12, 31, 23, 59, 999)
Gets next century start date from a given date.
import { getNextCenturyStart } from '@wojtekmaj/date-utils';
getNextCenturyStart(new Date(2019, 0, 1)); // new Date(2101, 0, 1)
Gets next century start date from a given date.
import { getNextCenturyEnd } from '@wojtekmaj/date-utils';
getNextCenturyEnd(new Date(2019, 0, 1)); // new Date(2200, 12, 31, 23, 59, 999)
Gets century start and end dates from a given date. Returns an array of values equal to the ones returned by getCenturyStart()
and getCenturyEnd()
.
import { getCenturyRange } from '@wojtekmaj/date-utils';
getCenturyRange(new Date(2019, 0, 1)); // [new Date(2001, 0, 1), new Date(2100, 12, 31, 23, 59, 999)
Gets decade start date from a given date.
import { getDecadeStart } from '@wojtekmaj/date-utils';
getDecadeStart(new Date(2019, 0, 1)); // new Date(2011, 0, 1)
Gets decade start date from a given date.
import { getDecadeEnd } from '@wojtekmaj/date-utils';
getDecadeEnd(new Date(2019, 0, 1)); // new Date(2020, 12, 31, 23, 59, 999)
Gets previous decade start date from a given date.
import { getPreviousDecadeStart } from '@wojtekmaj/date-utils';
getPreviousDecadeStart(new Date(2019, 0, 1)); // new Date(2001, 0, 1)
Gets decade start date from a given date.
import { getPreviousDecadeEnd } from '@wojtekmaj/date-utils';
getPreviousDecadeEnd(new Date(2019, 0, 1)); // new Date(2010, 12, 31, 23, 59, 999)
Gets next decade start date from a given date.
import { getNextDecadeStart } from '@wojtekmaj/date-utils';
getNextDecadeStart(new Date(2019, 0, 1)); // new Date(2021, 0, 1)
Gets next decade start date from a given date.
import { getNextDecadeEnd } from '@wojtekmaj/date-utils';
getNextDecadeEnd(new Date(2019, 0, 1)); // new Date(2030, 12, 31, 23, 59, 999)
Gets decade start and end dates from a given date. Returns an array of values equal to the ones returned by getDecadeStart()
and getDecadeEnd()
.
import { getDecadeRange } from '@wojtekmaj/date-utils';
getDecadeRange(new Date(2019, 0, 1)); // [new Date(2011, 0, 1), new Date(2020, 12, 31, 23, 59, 999)
Gets year start date from a given date.
import { getYearStart } from '@wojtekmaj/date-utils';
getYearStart(new Date(2019, 6, 1)); // new Date(2019, 0, 1)
Gets year start date from a given date.
import { getYearEnd } from '@wojtekmaj/date-utils';
getYearEnd(new Date(2019, 6, 1)); // new Date(2019, 12, 31, 23, 59, 999)
Gets previous year start date from a given date.
import { getPreviousYearStart } from '@wojtekmaj/date-utils';
getPreviousYearStart(new Date(2019, 6, 1)); // new Date(2018, 0, 1)
Gets year start date from a given date.
import { getPreviousYearEnd } from '@wojtekmaj/date-utils';
getPreviousYearEnd(new Date(2019, 6, 1)); // new Date(2018, 12, 31, 23, 59, 999)
Gets next year start date from a given date.
import { getNextYearStart } from '@wojtekmaj/date-utils';
getNextYearStart(new Date(2019, 6, 1)); // new Date(2020, 0, 1)
Gets next year start date from a given date.
import { getNextYearEnd } from '@wojtekmaj/date-utils';
getNextYearEnd(new Date(2019, 6, 1)); // new Date(2020, 12, 31, 23, 59, 999)
Gets year start and end dates from a given date. Returns an array of values equal to the ones returned by getYearStart()
and getYearEnd()
.
import { getYearRange } from '@wojtekmaj/date-utils';
getYearRange(new Date(2019, 6, 1)); // [new Date(2019, 0, 1), new Date(2019, 12, 31, 23, 59, 999)
Gets month start date from a given date.
import { getMonthStart } from '@wojtekmaj/date-utils';
getMonthStart(new Date(2019, 6, 15)); // new Date(2019, 6, 1)
Gets month start date from a given date.
import { getMonthEnd } from '@wojtekmaj/date-utils';
getMonthEnd(new Date(2019, 6, 15)); // new Date(2019, 6, 31, 23, 59, 999)
Gets previous month start date from a given date.
import { getPreviousMonthStart } from '@wojtekmaj/date-utils';
getPreviousMonthStart(new Date(2019, 6, 15)); // new Date(2019, 5, 1)
Gets month start date from a given date.
import { getPreviousMonthEnd } from '@wojtekmaj/date-utils';
getPreviousMonthEnd(new Date(2019, 6, 15)); // new Date(2019, 5, 30, 23, 59, 999)
Gets next month start date from a given date.
import { getNextMonthStart } from '@wojtekmaj/date-utils';
getNextMonthStart(new Date(2019, 6, 15)); // new Date(2019, 7, 1)
Gets next month start date from a given date.
import { getNextMonthEnd } from '@wojtekmaj/date-utils';
getNextMonthEnd(new Date(2019, 6, 15)); // new Date(2019, 7, 31, 23, 59, 999)
Gets month start and end dates from a given date. Returns an array of values equal to the ones returned by getMonthStart()
and getMonthEnd()
.
import { getMonthRange } from '@wojtekmaj/date-utils';
getMonthRange(new Date(2019, 6, 15)); // [new Date(2019, 6, 1), new Date(2019, 6, 31, 23, 59, 999)
Gets day start date from a given date.
import { getDayStart } from '@wojtekmaj/date-utils';
getDayStart(new Date(2019, 6, 15, 12)); // new Date(2019, 6, 15)
Gets day start date from a given date.
import { getDayEnd } from '@wojtekmaj/date-utils';
getDayEnd(new Date(2019, 6, 15, 12)); // new Date(2019, 6, 15, 23, 59, 999)
Gets previous day start date from a given date.
import { getPreviousDayStart } from '@wojtekmaj/date-utils';
getPreviousDayStart(new Date(2019, 6, 15, 12)); // new Date(2019, 6, 14)
Gets day start date from a given date.
import { getPreviousDayEnd } from '@wojtekmaj/date-utils';
getPreviousDayEnd(new Date(2019, 6, 15, 12)); // new Date(2019, 6, 14, 23, 59, 999)
Gets next day start date from a given date.
import { getNextDayStart } from '@wojtekmaj/date-utils';
getNextDayStart(new Date(2019, 6, 15, 12)); // new Date(2019, 6, 16)
Gets next day start date from a given date.
import { getNextDayEnd } from '@wojtekmaj/date-utils';
getNextDayEnd(new Date(2019, 6, 15, 12)); // new Date(2019, 6, 16, 23, 59, 999)
Gets day start and end dates from a given date. Returns an array of values equal to the ones returned by getDayStart()
and getDayEnd()
.
import { getDayRange } from '@wojtekmaj/date-utils';
getDayRange(new Date(2019, 6, 15, 12)); // [new Date(2019, 6, 15), new Date(2019, 6, 15, 23, 59, 999)
Gets number of days in a month from a given date.
import { getDaysInMonth } from '@wojtekmaj/date-utils';
getDaysInMonth(new Date(2019, 0, 15)); // 31
Returns local hours and minutes (hh:mm).
import { getHoursMinutes } from '@wojtekmaj/date-utils';
getHoursMinutes(new Date(2019, 0, 15, 16, 4)); // "16:04"
Returns local hours, minutes and seconds (hh:mm:ss).
import { getHoursMinutesSeconds } from '@wojtekmaj/date-utils';
getHoursMinutesSeconds(new Date(2019, 0, 15, 16, 4, 41)); // "16:04:41"
Returns local month in ISO-like format (YYYY-MM).
import { getISOLocalMonth } from '@wojtekmaj/date-utils';
getISOLocalMonth(new Date(2019, 0, 15)); // "2019-01"
Returns local date in ISO-like format (YYYY-MM-DD).
import { getISOLocalDate } from '@wojtekmaj/date-utils';
getISOLocalDate(new Date(2019, 0, 15)); // "2019-01-15"
Returns local date & time in ISO-like format (YYYY-MM-DDThh:mm:ss).
import { getISOLocalDateTime } from '@wojtekmaj/date-utils';
getISOLocalDateTime(new Date(2019, 0, 15, 16, 4, 41)); // "2019-01-15T16:04:41"
The MIT License.
Wojciech Maj |