Date Calculations
The Date Math package provides options for adding and removing specific time periods (for example, days and weeks), getting the first or last time periods (for example, the first date in a month), and also creating dates and comparing dates.
Adding and Removing Time Periods
Days
To add or remove a conditional number of days from a Date
object, use the addDays
method.
import { addDays } from '@progress/kendo-date-math';
const date = new Date(2000, 10, 10);
const newDate = addDays(date, 10); // Returns a new Date instance.
Weeks
To add or remove a conditional number of weeks from a Date
object, use the addWeeks
method.
import { addWeeks } from '@progress/kendo-date-math';
const date = new Date(2000, 10, 10);
const newDate = addWeeks(date, 10); // Returns a new Date instance.
Months
To add or remove a conditional number of months from a Date
object, use the addMonths
method.
import { addMonths } from '@progress/kendo-date-math';
const date = new Date(2000, 10, 10);
const newDate = addMonths(date, 10); // Returns a new Date instance `2001-9-10`.
Years
To add or remove a conditional number of years from a Date
object, use the addYears
method.
import { addYears } from '@progress/kendo-date-math';
const date = new Date(2000, 10, 10);
const newDate = addYears(date, 10); // Returns a new Date instance `2010-11-10`.
Decades
To add or remove a conditional number of decades from a Date
object, use the addDecades
method.
import { addDecades } from '@progress/kendo-date-math';
const date = new Date(2000, 10, 10);
const newDate = addDecades(date, 10); // Returns a new Date instance `2100-11-10`.
Centuries
To add or remove a conditional number of centuries from a Date
object, use the addCenturies
method.
import { addCenturies } from '@progress/kendo-date-math';
const date = new Date(2000, 10, 10);
const newDate = addCenturies(date, 10); // Returns a new Date instance `3000-11-10`.
Getting First or Last Time Periods
First and Last Dates in Months
To get the first and last dates from a Date
object, use the firstDayOfMonth
and lastDayOfMonth
functions respectively.
import { firstDayOfMonth, lastDayOfMonth } from '@progress/kendo-date-math';
const date = new Date(2000, 10, 10);
const firstDay = firstDayOfMonth(date); // Returns first date of the month, `2000-11-1`.
const lastDay = lastDayOfMonth(date); // Returns last date of the month, `2000-11-30`.
First and Last Months in Years
To get the first and last months from a Date
object, use the firstMonthOfYear
and lastMonthOfYear
functions respectively.
import { firstMonthOfYear, lastMonthOfYear } from '@progress/kendo-date-math';
const date = new Date(2000, 10, 10);
const firstDay = firstMonthOfYear(date); // Returns first month of the year, `2000-1-10`.
const lastDay = lastMonthOfYear(date); // Returns last month of the year, `2000-12-10`.
First and Last Years in Decades
To get the first and last years from a Date
object, use the firstYearOfDecade
and lastYearOfDecade
functions respectively.
import { firstYearOfDecade, lastYearOfDecade } from '@progress/kendo-date-math';
const date = new Date(2005, 10, 10);
const firstDay = firstYearOfDecade(date); // Returns first year of the decade, `2000-11-10`.
const lastDay = lastYearOfDecade(date); // Returns last year of the decade, `2009-11-10`.
First and Last Decades in Centuries
To get the first and last decades from a Date
object, use the firstDecadeOfCentury
and lastDecadeOfCentury
functions respectively.
import { firstDecadeOfCentury, lastDecadeOfCentury } from '@progress/kendo-date-math';
const date = new Date(2020, 10, 10);
const firstDay = firstDecadeOfCentury(date); // Returns first decade of the century, `2000-11-10`.
const lastDay = lastDecadeOfCentury(date); // Returns last decade of the century, `2090-11-10`.
Getting Durations and Specific Time Periods
- Next or previous dates in a week
- Week numbers in a year
- Durations between two dates within a month
- Durations between two dates within a year
- Durations between two dates within a decade
- Durations between two dates within a century
- Date parts of the
Date
instance
Next and Previous Dates in Weeks
To get the next and previous dates in a week based on a week day number, use the nextDayOfWeek
and prevDayOfWeek
functions respectively.
import { Day, prevDayOfWeek, nextDayOfWeek } from '@progress/kendo-date-math';
const nextDate = nextDayOfWeek(new Date(2016, 0, 1), Day.Wednesday); // 2016-01-06, Wednesday
const prevDate = prevDayOfWeek(new Date(2016, 0, 1), Day.Wednesday); // 2015-12-30, Wednesday
Week Numbers in Years
To calculate the week number, based on a given date, use the weekInYear
function.
import { weekInYear } from '@progress/kendo-date-math';
const date = new Date(2000, 10, 10);
const weekNumber = weekInYear(date); // Returns the week number of the corresponding date.
Date Ranges in Months
To get the duration in months between two Date
objects, use the durationInMonths
function.
import { durationInMonths } from '@progress/kendo-date-math';
const start = new Date(2020, 1, 20);
const end = new Date(2020, 10, 4);
const duration = durationInMonths(start, end); // Returns the duration in months, `9`.
Date Ranges in Years
To get the duration in years between two Date
objects, use the durationInYears
function.
import { durationInYears } from '@progress/kendo-date-math';
const start = new Date(2015, 1, 20);
const end = new Date(2020, 10, 4);
const duration = durationInYears(start, end); // Returns the duration in years, `5`.
Date Ranges in Decades
To get the duration in decades between two Date
objects, use the durationInDecades
function.
import { durationInDecades } from '@progress/kendo-date-math';
const start = new Date(2000, 1, 20);
const end = new Date(2020, 10, 4);
const duration = durationInDecades(start, end); // Returns the duration in decades, `2`.
Date Ranges in Centuries
To get the centuries in decades between two Date
objects, use the durationInCenturies
function.
import { durationInCenturies } from '@progress/kendo-date-math';
const start = new Date(2000, 1, 20);
const end = new Date(2300, 10, 4);
const duration = durationInCenturies(start, end); // Returns the duration in centuries, `3`.
Date Part
To get only the date part of a Date
instance, use the getDate
function.
import { getDate } from '@progress/kendo-date-math';
const date = new Date(2000, 10, 10, 22, 30);
const datePart = getDate(date); // Returns a Date instance with '00:00:00' time.
Creating Dates
To create a date with year value below 100, use the createDate
function.
import { createDate } from '@progress/kendo-date-math';
createDate(16, 0, 15); // 0016-01-15 00:00:00
createDate(2016, 0, 15); // 2016-01-15 00:00:00
createDate(2016, 0, 15, 22, 22, 20); // 2016-01-15 22:22:20
Comparing Dates
- Comparing the date and time portions of the
Date
instances. - Comparing only the date portions of the
Date
instances.
Date and Time Portions
To compare the date and time portions of the date
instance of two dates, use isEqual
function.
import { isEqual } from '@progress/kendo-date-math';
const date1 = new Date(2000, 10, 10);
const date2 = new Date(2000, 10, 10, 10);
const equal = isEqual(date1, date2); // Returns false.
Date-Only Portions
To compare only the date portions of the date
instance of two dates, use the isEqualDate
function.
import { isEqualDate } from '@progress/kendo-date-math';
const date1 = new Date(2000, 10, 10);
const date2 = new Date(2000, 10, 10, 10);
const equal = isEqualDate(date1, date2); // Returns true.