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