Skip to content

Latest commit

 

History

History
2084 lines (1301 loc) · 41.9 KB

File metadata and controls

2084 lines (1301 loc) · 41.9 KB

Table of Contents

addDays

Adds days to the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • days number the amount of days to add

Examples

import { addDays } from 'tempus';

// => input: 2020-11-18 15:30:00
addDays(new Date(2020, 10, 18, 15, 30), 3);
// => output: 2020-11-21 15:30:00

Returns Date the new calculated date

addHours

Adds hours to the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • hours number the amount of hours to add

Examples

import { addHours } from 'tempus';

// => input: 2020-11-18 15:30:00
addHours(new Date(2020, 10, 18, 15, 30), 3);
// => output: 2020-11-18 18:30:00

Returns Date the new calculated date

addMilliseconds

Adds milliseconds to the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • milliseconds number the amount of milliseconds to add

Examples

import { addMilliseconds } from 'tempus';

// => input: 1605654000000 ms
addMilliseconds(new Date(2020, 10, 18), 100);
// => output: 1605654000100 ms

Returns Date the new calculated date

addMinutes

Adds minutes to the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • minutes number the amount of minutes to add

Examples

import { addMinutes } from 'tempus';

// => input: 2020-11-18 15:30:00
addMinutes(new Date(2020, 10, 18, 15, 30), 50);
// => output: 2020-11-18 16:20:00

Returns Date the new calculated date

addMonths

Adds months to the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • months number the amount of months to add

Examples

import { addMonths } from 'tempus';

// => input: 2020-11-18 15:30:00
addMonths(new Date(2020, 10, 18, 15, 30), 1);
// => output: 2020-12-18 15:30:00

Returns Date the new calculated date

addQuarters

Adds quarters to the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • quarters number the amount of quarters to add

Examples

import { addQuarters } from 'tempus';

// => input: 2020-11-18 15:30:00
addQuarters(new Date(2020, 10, 18, 15, 30), 1);
// => output: 2020-02-18 15:30:00

Returns Date the new calculated date

addSeconds

Adds seconds to the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • seconds number the amount of seconds to add

Examples

import { addSeconds } from 'tempus';

// => input: 1605654000000 ms
addSeconds(new Date(2020, 10, 18), 50);
// => output: 1605654050000 ms

Returns Date the new calculated date

addWeeks

Adds weeks to the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • weeks number the amount of weeks to add

Examples

import { addWeeks } from 'tempus';

// => input: 2020-11-18 15:30:00
addWeeks(new Date(2020, 10, 18, 15, 30), 1);
// => output: 2020-11-25 15:30:00

Returns Date the new calculated date

addYears

Adds years to the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • years number the amount of years to add

Examples

import { addYears } from 'tempus';

// => input: 2020-11-18 15:30:00
addYears(new Date(2020, 10, 18, 15, 30), 2);
// => output: 2022-02-18 15:30:00

Returns Date the new calculated date

differenceInDays

Calculates the difference between two date objects in days

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { differenceInDays } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30);
const other = new Date(2018, 3, 18, 20, 15, 0, 0);

differenceInDays(value, other);
// => output: 945d

Returns number the difference in days

differenceInHours

Calculates the difference between two date objects in hours

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { differenceInHours } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30);
const other = new Date(2018, 3, 18, 20, 15, 0, 0);

differenceInHours(value, other);
// => output: 22676h

Returns number the difference in hours

differenceInMilliseconds

Calculates the difference between two date objects in milliseconds

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { differenceInMilliseconds } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30);
const other = new Date(2018, 3, 18, 20, 15, 0, 0);

differenceInMilliseconds(value, other);
// => output: 81634545060ms

Returns number the difference in milliseconds

differenceInMinutes

Calculates the difference between two date objects in minutes

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { differenceInMinutes } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30);
const other = new Date(2018, 3, 18, 20, 15, 0, 0);

differenceInMinutes(value, other);
// => output: 1360576min

Returns number the difference in minutes

differenceInMonths

Calculates the difference between two date objects in months

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { differenceInMonths } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30);
const other = new Date(2018, 3, 18, 20, 15, 0, 0);

differenceInMonths(value, other);
// => output: 31m

Returns number the difference in months

differenceInQuarters

Calculates the difference between two date objects in quarters

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { differenceInQuarters } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30);
const other = new Date(2018, 3, 18, 20, 15, 0, 0);

differenceInQuarters(value, other);
// => output: 10q

Returns number the difference in quarters

differenceInSeconds

Calculates the difference between two date objects in seconds

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { differenceInSeconds } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30);
const other = new Date(2018, 3, 18, 20, 15, 0, 0);

differenceInSeconds(value, other);
// => output: 81634545s

Returns number the difference in seconds

differenceInWeeks

Calculates the difference between two date objects in weeks

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { differenceInWeeks } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30);
const other = new Date(2018, 3, 18, 20, 15, 0, 0);

differenceInWeeks(value, other);
// => output: 135w

Returns number the difference in weeks

differenceInYears

Calculates the difference between two date objects in years

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { differenceInYears } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30);
const other = new Date(2018, 3, 18, 20, 15, 0, 0);

differenceInYears(value, other);
// => output: 2y

Returns number the difference in years

getDayOfYear

Calculates the number of days that passed since start of the year.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { getDayOfYear } from 'tempus';

// => input: 2020-11-18 15:30:00
getDayOfYear(new Date(2020, 10, 18, 15, 30));
// => output: 323

Returns number the number of days

getQuarterOfYear

Calculates the number of quarters that passed since start of the year.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { getQuarterOfYear } from 'tempus';

// => input: 2020-11-18 15:30:00
getQuarterOfYear(new Date(2020, 10, 18, 15, 30));
// => output: 4

Returns number the number of quarters

getWeekOfYear

Calculates the number of weeks that passed since start of the year.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { getWeekOfYear } from 'tempus';

// => input: 2020-11-18 15:30:00
getWeekOfYear(new Date(2020, 10, 18, 15, 30));
// => output: 47

Returns number the number of weeks

Interval

An interval

Type: Object

Properties

  • start Date the start of the interval
  • end Date the end of the interval

isAfter

Checks if the first date object is after the second date object.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { isAfter } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30, 45);
const other = new Date(2019, 11, 6, 16, 35, 50);

isAfter(value, other);
// => output: true

Returns boolean true if the first date is after second date

isBefore

Checks if the first date object is before the second date object.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { isBefore } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30, 45);
const other = new Date(2019, 11, 6, 16, 35, 50);

isBefore(value, other);
// => output: false

Returns boolean true if the first date is before second date

isBetween

Checks if the specified date is in the interval.

Parameters

Examples

import { isBetween } from 'tempus';

isBetween(new Date(2020, 10, 18, 15, 30, 45), {
    start: new Date(2020, 10, 1),
    end: new Date(2020, 11, 1)
});
// => output: true

Returns boolean true if the date is in the interval

isCurrentDay

Checks if the specified date has the same amount of days as the current date.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isCurrentDay } from 'tempus';

isCurrentDay(new Date(2020, 10, 18, 15, 30, 45));

Returns boolean true if the date has the same amount of days

isCurrentHour

Checks if the specified date has the same amount of hours as the current date.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isCurrentHour } from 'tempus';

isCurrentHour(new Date(2020, 10, 18, 15, 30, 45));

Returns boolean true if the date has the same amount of hours

isCurrentMinute

Checks if the specified date has the same amount of minutes as the current date.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isCurrentMinute } from 'tempus';

isCurrentMinute(new Date(2020, 10, 18, 15, 30, 45));

Returns boolean true if the date has the same amount of minutes

isCurrentMonth

Checks if the specified date is in same month as the current date.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isCurrentMonth } from 'tempus';

isCurrentMonth(new Date(2020, 10, 18, 15, 30, 45));

Returns boolean true if the date is in same month

isCurrentQuarter

Checks if the specified date is in same quarter as the current date.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isCurrentQuarter } from 'tempus';

isCurrentQuarter(new Date(2020, 10, 18, 15, 30, 45));

Returns boolean true if the date is in same quarter

isCurrentSecond

Checks if the specified date has the same amount of seconds as the current date.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isCurrentSecond } from 'tempus';

isCurrentSecond(new Date(2020, 10, 18, 15, 30, 45));

Returns boolean true if the date has the same amount of seconds

isCurrentWeek

Checks if the specified date is in same week as the current date.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isCurrentWeek } from 'tempus';

isCurrentWeek(new Date(2020, 10, 18, 15, 30, 45));

Returns boolean true if the date is in same week

isCurrentWeekday

Checks if the specified date has the same weekday as the current date.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isCurrentWeekday } from 'tempus';

isCurrentWeekday(new Date(2020, 10, 18, 15, 30, 45));

Returns boolean true if the date has the same weekday

isCurrentYear

Checks if the specified date is in same year as the current date.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isCurrentYear } from 'tempus';

isCurrentYear(new Date(2020, 10, 18, 15, 30, 45));

Returns boolean true if the date is in same year

isDate

Checks if an object is an instance of Date.

Parameters

  • value any the object to check

Returns boolean true if the object is a date

isEqual

Checks if two date objects are equal.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { isEqual } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30, 45);
const other = new Date(2020, 10, 18, 15, 30, 45);

isEqual(value, other);
// => output: true

Returns boolean true if the dates are equal

isFriday

Checks if the specified date is on a friday.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isFriday } from 'tempus';

// => input: 2020-11-18 15:30:00
isFriday(new Date(2020, 10, 18, 15, 30));
// => output: false

Returns boolean true if the date is on a friday

isFuture

Checks if the specified date is in the future.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isFuture } from 'tempus';

isFuture(new Date(2020, 10, 18, 15, 30, 45));

Returns boolean true if the date is in the future

isLeapYear

Checks if the specified date is in a leap year.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isLeapYear } from 'tempus';

// => input: 2020-11-18 15:30:00
isLeapYear(new Date(2020, 10, 18, 15, 30));
// => output: true

Returns boolean true if the date is in a leap year

isMonday

Checks if the specified date is on a monday.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isMonday } from 'tempus';

// => input: 2020-11-18 15:30:00
isMonday(new Date(2020, 10, 18, 15, 30));
// => output: false

Returns boolean true if the date is on a monday

isPast

Checks if the specified date is in the past.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isPast } from 'tempus';

isPast(new Date(2020, 10, 18, 15, 30, 45));

Returns boolean true if the date is in the past

isSameDay

Checks if two date objects have the same amount of days.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { isSameDay } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30, 45);
const other = new Date(2020, 10, 18, 15, 30, 45);

isSameDay(value, other);
// => output: true

Returns boolean true if the dates have the same amount of days

isSameHour

Checks if two date objects have the same amount of hours.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { isSameHour } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30, 45);
const other = new Date(2020, 10, 18, 15, 30, 45);

isSameHour(value, other);
// => output: true

Returns boolean true if the dates have the same amount of hours

isSameMinute

Checks if two date objects have the same amount of minutes.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { isSameMinute } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30, 45);
const other = new Date(2020, 10, 18, 15, 30, 45);

isSameMinute(value, other);
// => output: true

Returns boolean true if the dates have the same amount of minutes

isSameMonth

Checks if two date objects are in the same month.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { isSameMonth } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30, 45);
const other = new Date(2020, 10, 18, 15, 30, 45);

isSameMonth(value, other);
// => output: true

Returns boolean true if the dates are in the same month

isSameQuarter

Checks if two date objects are in the same quarter.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { isSameQuarter } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30, 45);
const other = new Date(2020, 10, 18, 15, 30, 45);

isSameQuarter(value, other);
// => output: true

Returns boolean true if the dates are in the same quarter

isSameSecond

Checks if two date objects have the same amount of seconds.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { isSameSecond } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30, 45);
const other = new Date(2020, 10, 18, 15, 30, 45);

isSameSecond(value, other);
// => output: true

Returns boolean true if the dates have the same amount of seconds

isSameWeek

Checks if two date objects are in the same week.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { isSameWeek } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30, 45);
const other = new Date(2020, 10, 18, 15, 30, 45);

isSameWeek(value, other);
// => output: true

Returns boolean true if the dates are in the same week

isSameWeekday

Checks if two date objects have the same weekday.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { isSameWeekday } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30, 45);
const other = new Date(2020, 10, 18, 15, 30, 45);

isSameWeekday(value, other);
// => output: true

Returns boolean true if the dates have the same amount of weekday

isSameYear

Checks if two date objects are in the same year.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • other (Date | number) a date object or a timestamp in milliseconds

Examples

import { isSameYear } from 'tempus';

const value = new Date(2020, 10, 18, 15, 30, 45);
const other = new Date(2020, 10, 18, 15, 30, 45);

isSameYear(value, other);
// => output: true

Returns boolean true if the dates are in the same year

isSaturday

Checks if the specified date is on a saturday.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isSaturday } from 'tempus';

// => input: 2020-11-18 15:30:00
isSaturday(new Date(2020, 10, 18, 15, 30));
// => output: false

Returns boolean true if the date is on a saturday

isSunday

Checks if the specified date is on a sunday.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isSunday } from 'tempus';

// => input: 2020-11-18 15:30:00
isSunday(new Date(2020, 10, 18, 15, 30));
// => output: false

Returns boolean true if the date is on a sunday

isThursday

Checks if the specified date is on a thursday.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isThursday } from 'tempus';

// => input: 2020-11-18 15:30:00
isThursday(new Date(2020, 10, 18, 15, 30));
// => output: true

Returns boolean true if the date is on a thursday

isTomorrow

Checks if the specified date is tomorrow.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isTomorrow } from 'tempus';

isTomorrow(new Date(2020, 10, 18, 15, 30, 45));

Returns boolean true if the date is tomorrow

isTuesday

Checks if the specified date is on a tuesday.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isTuesday } from 'tempus';

// => input: 2020-11-18 15:30:00
isTuesday(new Date(2020, 10, 18, 15, 30));
// => output: false

Returns boolean true if the date is on a tuesday

isWednesday

Checks if the specified date is on a wednesday.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isWednesday } from 'tempus';

// => input: 2020-11-18 15:30:00
isWednesday(new Date(2020, 10, 18, 15, 30));
// => output: false

Returns boolean true if the date is on a wednesday

isYesterday

Checks if the specified date is yesterday.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds

Examples

import { isYesterday } from 'tempus';

isYesterday(new Date(2020, 10, 18, 15, 30, 45));

Returns boolean true if the date is yesterday

max

Selects the maximum date in an array of dates.

Parameters

  • values Date an array of date objects

Examples

import { max } from 'tempus';

const dates = [
    new Date(2020, 0, 30, 15, 30, 45),
    new Date(2020, 5, 25, 15, 30, 45),
];

max(dates);
// => output: 2020-06-05 15:30:45

Returns Date the maximum date object

min

Selects the minimum date in an array of dates.

Parameters

  • values Date an array of date objects

Examples

import { min } from 'tempus';

const dates = [
    new Date(2020, 0, 30, 15, 30, 45),
    new Date(2020, 5, 25, 15, 30, 45),
];

min(dates);
// => output: 2020-01-30 15:30:45

Returns Date the minimum date object

subtractDays

Subtracts days from the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • days number the amount of days to add

Examples

import { subtractDays } from 'tempus';

// => input: 2020-11-18 15:30:00
subtractDays(new Date(2020, 10, 18, 15, 30), 3);
// => output: 2020-11-15 15:30:00

Returns Date the new calculated date

subtractHours

Subtracts hours from the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • hours number the amount of hours to add

Examples

import { subtractHours } from 'tempus';

// => input: 2020-11-18 15:30:00
subtractHours(new Date(2020, 10, 18, 15, 30), 3);
// => output: 2020-11-18 12:30:00

Returns Date the new calculated date

subtractMilliseconds

Subtracts milliseconds from the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • milliseconds number the amount of milliseconds to add

Examples

import { subtractMilliseconds } from 'tempus';

// => input: 1605654000000 ms
subtractMilliseconds(new Date(2020, 10, 18), 100);
// => output: 1605653999900 ms

Returns Date the new calculated date

subtractMinutes

Subtracts minutes from the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • minutes number the amount of minutes to add

Examples

import { subtractMinutes } from 'tempus';

// => input: 2020-11-18 15:30:00
subtractMinutes(new Date(2020, 10, 18, 15, 30), 50);
// => output: 2020-11-18 14:40:00

Returns Date the new calculated date

subtractMonths

Subtracts months from the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • months number the amount of months to add

Examples

import { subtractMonths } from 'tempus';

// => input: 2020-11-18 15:30:00
subtractMonths(new Date(2020, 10, 18, 15, 30), 1);
// => output: 2020-10-18 15:30:00

Returns Date the new calculated date

subtractQuarters

Subtracts quarters from the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • quarters number the amount of quarters to add

Examples

import { subtractQuarters } from 'tempus';

// => input: 2020-11-18 15:30:00
subtractQuarters(new Date(2020, 10, 18, 15, 30), 1);
// => output: 2020-08-18 15:30:00

Returns Date the new calculated date

subtractSeconds

Subtracts seconds from the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • seconds number the amount of seconds to add

Examples

import { subtractSeconds } from 'tempus';

// => input: 1605653950000 ms
subtractSeconds(new Date(2020, 10, 18), 50);
// => output: 1605654050000 ms

Returns Date the new calculated date

subtractWeeks

Subtracts weeks from the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • weeks number the amount of weeks to add

Examples

import { addWeeks } from 'tempus';

// => input: 2020-11-18 15:30:00
subtractWeeks(new Date(2020, 10, 18, 15, 30), 1);
// => output: 2020-11-11 15:30:00

Returns Date the new calculated date

subtractYears

Subtracts years from the date object and returns a new instance, so the original object stays unchanged.

Parameters

  • value (Date | number) a date object or a timestamp in milliseconds
  • years number the amount of years to add

Examples

import { subtractYears } from 'tempus';

// => input: 2020-11-18 15:30:00
subtractYears(new Date(2020, 10, 18, 15, 30), 2);
// => output: 2022-02-18 15:30:00

Returns Date the new calculated date

toDate

Converts any type to a date object. If the specified value is already a date, it returns just the value. If the value is a number, it will be converted with Date(milliseconds) constuctor. If the value anything else, a invalid date will be returned.

Parameters

  • value any the type to convert to a date

Examples

import { toDate } from 'tempus';

// => input: 2020-11-18 15:30:00
toDate(new Date(2020, 10, 18, 15, 30));
// => output: 2020-11-18 15:30:00

// => input: 1605709845060
toDate(1605709845060);
// => output: 2020-11-18 15:30:00

// => input: "Hello World"
toDate("Hello World");
// => output: Date(NaN) => Invalid date

Returns Date the converted date object