Skip to content

Commit

Permalink
Merge pull request #30 from funbox/bugfix/get-start-of-month
Browse files Browse the repository at this point in the history
Fix setDate before setMonth
  • Loading branch information
igoradamenko committed Apr 4, 2023
2 parents a4ceede + 6bddcf0 commit 83c0eeb
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## 5.0.1 (04.04.2023)

Fixed `getStartOfMonth` which returned wrong value when the second argument is passed
and the difference between the current and the desired month is not equal to 0.


## 5.0.0 (27.01.2023)

Added alternative functions to deal with both full and calendar days, months, years.
Expand Down
3 changes: 2 additions & 1 deletion lib/getStartOfMonth.ts
Expand Up @@ -9,11 +9,12 @@ import { ChronosDate, ensureDate } from './helpers/ensureDate';
export default (value: ChronosDate, diff = 0): Date => {
const date = ensureDate(value);

date.setDate(1);

if (diff) {
date.setMonth(date.getMonth() + diff);
}

date.setDate(1);
date.setHours(0, 0, 0, 0);

return date;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@funboxteam/chronos",
"version": "5.0.0",
"version": "5.0.1",
"description": "One library to rule the time",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
4 changes: 4 additions & 0 deletions tests/index.ts
Expand Up @@ -527,6 +527,10 @@ describe('getStartOfMinutes, getStartOfHours, getStartOfDay, getStartOfWeek, get
expect(getStartOfMonth(new Date(2020, 2, 2, 23, 59), -1)).to.equalTime(new Date(2020, 1, 1, 0, 0));
});

it('returns date of start of month - 1', () => {
expect(getStartOfMonth(new Date(2023, 2, 31, 23, 59), -1)).to.equalTime(new Date(2023, 1, 1, 0, 0));
});

it('returns date of start of year', () => {
expect(getStartOfYear(new Date(2020, 1, 1, 23, 59))).to.equalTime(new Date(2020, 0, 1, 0, 0));
});
Expand Down

0 comments on commit 83c0eeb

Please sign in to comment.