# Date Calculations

The Date Math package provides options for [adding and removing specific time periods (for example, days and weeks)](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#adding-and-removing-time-periods), [getting the first or last time periods (for example, the first date in a month)](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#getting-first-or-last-time-periods), and also [creating dates](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#creating-dates) and [comparing dates](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#comparing-dates).

## Adding and Removing Time Periods

* [Days](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#days)
* [Weeks](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#weeks)
* [Months](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#months)
* [Years](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#years)
* [Decades](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#decades)
* [Centuries](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#centuries)

### Days

To add or remove a conditional number of days from a `Date` object, use the `addDays` method.

```ts
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.

```ts
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.

```ts
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.

```ts
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.

```ts
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.

```ts
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

* [Dates in a month](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#first-and-last-dates-in-months)
* [Months in a year](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#first-and-last-months-in-years)
* [Years in a decade](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#first-and-last-years-in-decades)
* [Decades in a century](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#first-and-last-decades-in-centuries)

### First and Last Dates in Months

To get the first and last dates from a `Date` object, use the `firstDayOfMonth` and `lastDayOfMonth` functions respectively.

```ts
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.

```ts
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.

```ts
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.

```ts
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](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#next-and-previous-dates-in-weeks)
* [Week numbers in a year](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#week-numbers-in-years)
* [Durations between two dates within a month](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#date-ranges-in-months)
* [Durations between two dates within a year](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#date-ranges-in-years)
* [Durations between two dates within a decade](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#date-ranges-in-decades)
* [Durations between two dates within a century](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#date-ranges-in-centuries)
* [Date parts of the `Date` instance](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#date-part)

### 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.

```ts
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.

```ts
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.

```ts
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.

```ts
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.

```ts
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.

```ts
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.

```ts
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.

```ts
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](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#date-and-time-portions) of the `Date` instances.
* [Comparing only the date portions](https://www.telerik.com/kendo-react-ui-develop/components/datemath/date-calculations#date-only-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.

```ts
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.

```ts
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.
```

## Suggested Links

* [Date Math API Index](https://www.telerik.com/kendo-react-ui-develop/components/datemath/api)
