Skip to content

Commit

Permalink
Merge pull request #29 from funbox/add-alt-functions
Browse files Browse the repository at this point in the history
Add alternative functions to deal with both full and calendar days, months, years
  • Loading branch information
igoradamenko committed Jan 27, 2023
2 parents 6b5829c + 78fa89c commit a4ceede
Show file tree
Hide file tree
Showing 16 changed files with 284 additions and 141 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,14 @@
# Changelog

## 5.0.0 (27.01.2023)

Added alternative functions to deal with both full and calendar days, months, years.

Removed `getWeek` function.

Colsult the [migration guide](./MIGRATION.md) for more.


## 4.1.2 (13.12.2022)

Fixed rounding issues for negative values in `getDiffInDays`, `getDiffInHours` & `getDiffInMinutes`.
Expand Down
11 changes: 11 additions & 0 deletions MIGRATION.md
@@ -1,5 +1,16 @@
# Migration

## 4.1.2 → 5.0.0

If you used `getWeek`, then make sure you've replaced this function with your own implementation.

If you used [`getDiffInMonths`](lib/getDiffInMonths.ts) or [`getDiffInYears`](lib/getDiffInYears.ts),
then check their sources. Ensure that they work exactly as you expect. It's possible that instead you'd like to use
[`getDiffInCalendarMonths`](lib/getDiffInCalendarMonths.ts) and [`getDiffInCalendarYears`](lib/getDiffInCalendarYears.ts).

Everything else should work as before.


## 3.3.0 → 4.0.0

Nothing changed.
Expand Down
81 changes: 36 additions & 45 deletions README.md
Expand Up @@ -53,24 +53,25 @@ Step by step those functions have evolved into Chronos—simple yet useful date
- [ChronosDate](#chronosdate)
- [Duration](#duration)
- [Functions](#functions)
- [addMinutes](#addminutes-addhours-adddays-addmonths-addyears)
- [addHours](#addminutes-addhours-adddays-addmonths-addyears)
- [addDays](#addminutes-addhours-adddays-addmonths-addyears)
- [addMonths](#addminutes-addhours-adddays-addmonths-addyears)
- [addYears](#addminutes-addhours-adddays-addmonths-addyears)
- [subtractMinutes](#subtractminutes-subtracthours-subtractdays-subtractmonths-subtractyears)
- [subtractHours](#subtractminutes-subtracthours-subtractdays-subtractmonths-subtractyears)
- [subtractDays](#subtractminutes-subtracthours-subtractdays-subtractmonths-subtractyears)
- [subtractMonths](#subtractminutes-subtracthours-subtractdays-subtractmonths-subtractyears)
- [subtractYears](#subtractminutes-subtracthours-subtractdays-subtractmonths-subtractyears)
- [addMinutes](#addminutes-addhours-adddays-addmonths-addcalendarmonths-addyears)
- [addHours](#addminutes-addhours-adddays-addmonths-addcalendarmonths-addyears)
- [addDays](#addminutes-addhours-adddays-addmonths-addcalendarmonths-addyears)
- [addMonths](#addminutes-addhours-adddays-addmonths-addcalendarmonths-addyears)
- [addCalendarMonths](#addminutes-addhours-adddays-addmonths-addcalendarmonths-addyears)
- [addYears](#addminutes-addhours-adddays-addmonths-addcalendarmonths-addyears)
- [subtractMinutes](#subtractminutes-subtracthours-subtractdays-subtractmonths-subtractcalendarmonths-subtractyears)
- [subtractHours](#subtractminutes-subtracthours-subtractdays-subtractmonths-subtractcalendarmonths-subtractyears)
- [subtractDays](#subtractminutes-subtracthours-subtractdays-subtractmonths-subtractcalendarmonths-subtractyears)
- [subtractMonths](#subtractminutes-subtracthours-subtractdays-subtractmonths-subtractcalendarmonths-subtractyears)
- [subtractCalendarMonths](#subtractminutes-subtracthours-subtractdays-subtractmonths-subtractcalendarmonths-subtractyears)
- [subtractYears](#subtractminutes-subtracthours-subtractdays-subtractmonths-subtractcalendarmonths-subtractyears)
- [formatDate](#formatdate)
- [formatTimeString](#formattimestring)
- [getMinutes](#getminutes-gethours-getday-getweek-getmonth-getyear)
- [getHours](#getminutes-gethours-getday-getweek-getmonth-getyear)
- [getDay](#getminutes-gethours-getday-getweek-getmonth-getyear)
- [getWeek](#getminutes-gethours-getday-getweek-getmonth-getyear)
- [getMonth](#getminutes-gethours-getday-getweek-getmonth-getyear)
- [getYear](#getminutes-gethours-getday-getweek-getmonth-getyear)
- [getMinutes](#getminutes-gethours-getday-getmonth-getyear)
- [getHours](#getminutes-gethours-getday-getmonth-getyear)
- [getDay](#getminutes-gethours-getday-getmonth-getyear)
- [getMonth](#getminutes-gethours-getday-getmonth-getyear)
- [getYear](#getminutes-gethours-getday-getmonth-getyear)
- [getWeekdayName](#getweekdayname-getmonthname)
- [getMonthName](#getweekdayname-getmonthname)
- [getDuration](#getduration)
Expand All @@ -79,11 +80,14 @@ Step by step those functions have evolved into Chronos—simple yet useful date
- [isSameDay](#issameminute-issamehour-issameday-issamemonth-issameyear)
- [isSameMonth](#issameminute-issamehour-issameday-issamemonth-issameyear)
- [isSameYear](#issameminute-issamehour-issameday-issamemonth-issameyear)
- [getDiffInMinutes](#getdiffinminutes-getdiffinhours-getdiffindays-getdiffinmonths-getdiffinyears)
- [getDiffInHours](#getdiffinminutes-getdiffinhours-getdiffindays-getdiffinmonths-getdiffinyears)
- [getDiffInDays](#getdiffinminutes-getdiffinhours-getdiffindays-getdiffinmonths-getdiffinyears)
- [getDiffInMonths](#getdiffinminutes-getdiffinhours-getdiffindays-getdiffinmonths-getdiffinyears)
- [getDiffInYears](#getdiffinminutes-getdiffinhours-getdiffindays-getdiffinmonths-getdiffinyears)
- [getDiffInMinutes](#getdiffinminutes-getdiffinhours-getdiffindays-getdiffincalendardays-getdiffinmonths-getdiffincalendarmonths-getdiffinyears-getdiffincalendaryears)
- [getDiffInHours](#getdiffinminutes-getdiffinhours-getdiffindays-getdiffincalendardays-getdiffinmonths-getdiffincalendarmonths-getdiffinyears-getdiffincalendaryears)
- [getDiffInDays](#getdiffinminutes-getdiffinhours-getdiffindays-getdiffincalendardays-getdiffinmonths-getdiffincalendarmonths-getdiffinyears-getdiffincalendaryears)
- [getDiffInCalendarDays](#getdiffinminutes-getdiffinhours-getdiffindays-getdiffincalendardays-getdiffinmonths-getdiffincalendarmonths-getdiffinyears-getdiffincalendaryears)
- [getDiffInMonths](#getdiffinminutes-getdiffinhours-getdiffindays-getdiffincalendardays-getdiffinmonths-getdiffincalendarmonths-getdiffinyears-getdiffincalendaryears)
- [getDiffInCalendarMonths](#getdiffinminutes-getdiffinhours-getdiffindays-getdiffincalendardays-getdiffinmonths-getdiffincalendarmonths-getdiffinyears-getdiffincalendaryears)
- [getDiffInYears](#getdiffinminutes-getdiffinhours-getdiffindays-getdiffincalendardays-getdiffinmonths-getdiffincalendarmonths-getdiffinyears-getdiffincalendaryears)
- [getDiffInCalendarYears](#getdiffinminutes-getdiffinhours-getdiffindays-getdiffincalendardays-getdiffinmonths-getdiffincalendarmonths-getdiffinyears-getdiffincalendaryears)
- [getStartOfMinutes](#getstartofminutes-getstartofhours-getstartofday-getstartofweek-getstartofmonth-getstartofyear-getstartofdecade)
- [getStartOfHours](#getstartofminutes-getstartofhours-getstartofday-getstartofweek-getstartofmonth-getstartofyear-getstartofdecade)
- [getStartOfDay](#getstartofminutes-getstartofhours-getstartofday-getstartofweek-getstartofmonth-getstartofyear-getstartofdecade)
Expand Down Expand Up @@ -154,7 +158,7 @@ Type describing return value of functions that work with time intervals.
Every function is immutable and those which accept `Date` instances and return `Date` instance always return
new `Date` instance and do not mutate the passed one.

### [addMinutes](./lib/addMinutes.ts), [addHours](./lib/addHours.ts), [addDays](./lib/addDays.ts), [addMonths](./lib/addMonths.ts), [addYears](./lib/addYears.ts)
### [addMinutes](./lib/addMinutes.ts), [addHours](./lib/addHours.ts), [addDays](./lib/addDays.ts), [addMonths](./lib/addMonths.ts), [addCalendarMonths](./lib/addCalendarMonths.ts), [addYears](./lib/addYears.ts)

```typescript
(value: ChronosDate, quantity: number) => Date;
Expand All @@ -175,16 +179,12 @@ addYears(1577836800, 1); // 2021-01-01T00:00:00.000Z

addMonths(new Date(2020, 0, 1), 1); // == new Date(2020, 1, 1);
addMonths(new Date(2020, 0, 31), 1); // == new Date(2020, 2, 2);
```

#### Important notes

When adding months to date in JS one should mind the amount of days in the current and the result month.

E.g. adding 1 month to 31.01.2020 leads to getting 02.03.2020, not 29.02.2020, because there can't be 31.02.2020.

addCalendarMonths(new Date(2020, 0, 1), 1); // == new Date(2020, 1, 1);
addCalendarMonths(new Date(2020, 0, 31), 1); // == new Date(2020, 1, 29);
```

### [subtractMinutes](./lib/subtractMinutes.ts), [subtractHours](./lib/subtractHours.ts), [subtractDays](./lib/subtractDays.ts), [subtractMonths](./lib/subtractMonths.ts), [subtractYears](./lib/subtractYears.ts)
### [subtractMinutes](./lib/subtractMinutes.ts), [subtractHours](./lib/subtractHours.ts), [subtractDays](./lib/subtractDays.ts), [subtractMonths](./lib/subtractMonths.ts), [subtractCalendarMonths](./lib/subtractCalendarMonths.ts), [subtractYears](./lib/subtractYears.ts)

```typescript
(value: ChronosDate, quantity: number) => Date;
Expand All @@ -204,15 +204,11 @@ subtractDays(new Date('2020-01-01T00:00:00.000Z'), 1); // 2019-12-31T00:00:00.00
subtractYears(1577836800, 1); // 2019-01-01T00:00:00.000Z

subtractMonths(new Date(2020, 0, 1), 1); // == new Date(2019, 11, 1);
subtractMonths(new Date(2020, 1, 29), 1); // == new Date(2020, 0, 29);
```

#### Important notes

When subtracting months to date in JS one should mind the amount of days in the current and the result month.

E.g. subtracting 1 month to 29.02.2020 leads to getting 29.01.2020, not 31.01.2020.
subtractMonths(new Date(2020, 2, 31), 1); // == new Date(2020, 2, 2);

subtractCalendarMonths(new Date(2020, 0, 1), 1); // == new Date(2019, 11, 1);
subtractCalendarMonths(new Date(2020, 2, 31), 1); // == new Date(2020, 1, 29);
```

### [formatDate](./lib/formatDate.ts)

Expand Down Expand Up @@ -285,7 +281,7 @@ formatTimeString('22:00', 'HH:mm', 'HH:mm:ss'); // '22:00:00'
```


### [getMinutes](./lib/getMinutes.ts), [getHours](./lib/getHours.ts), [getDay](./lib/getDay.ts), [getWeek](./lib/getWeek.ts), [getMonth](./lib/getMonth.ts), [getYear](./lib/getYear.ts)
### [getMinutes](./lib/getMinutes.ts), [getHours](./lib/getHours.ts), [getDay](./lib/getDay.ts), [getMonth](./lib/getMonth.ts), [getYear](./lib/getYear.ts)

```typescript
(value: ChronosDate) => number;
Expand All @@ -304,11 +300,6 @@ getDay(new Date(2020, 0, 1)); // 1;
getYear(1577836800); // 2020
```

#### Important notes

`getWeek` returns number of the week starting from the beginning of the year.


### [getWeekdayName](./lib/getWeekdayName.ts), [getMonthName](./lib/getMonthName.ts)

```typescript
Expand Down Expand Up @@ -367,7 +358,7 @@ isSameYear(1577750400, 1577836800); // false
```


### [getDiffInMinutes](./lib/getDiffInMinutes.ts), [getDiffInHours](./lib/getDiffInHours.ts), [getDiffInDays](./lib/getDiffInDays.ts), [getDiffInMonths](./lib/getDiffInMonths.ts), [getDiffInYears](./lib/getDiffInYears.ts)
### [getDiffInMinutes](./lib/getDiffInMinutes.ts), [getDiffInHours](./lib/getDiffInHours.ts), [getDiffInDays](./lib/getDiffInDays.ts), [getDiffInCalendarDays](./lib/getDiffInCalendarDays.ts), [getDiffInMonths](./lib/getDiffInMonths.ts), [getDiffInCalendarMonths](./lib/getDiffInCalendarMonths.ts), [getDiffInYears](./lib/getDiffInYears.ts), [getDiffInCalendarYears](./lib/getDiffInCalendarYears.ts)

```typescript
(firstValue: ChronosDate, secondValue: ChronosDate) => number;
Expand Down

0 comments on commit a4ceede

Please sign in to comment.