Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add negative years support #2640

Merged
merged 10 commits into from
May 12, 2024

Conversation

klm-turing
Copy link
Contributor

@klm-turing klm-turing commented Apr 26, 2024

Add ability to parse negative years

This fix 2636 issue

@willismonroe
Copy link

This looks good, I added a test (in manipulate.test.js):

it('Add and subtract with negative years', () => {
  expect(dayjs('-2006').add(1, 'y')).toEqual(dayjs('-2005'))
  expect(dayjs('-2006').subtract(1, 'y')).toEqual(dayjs('-2007'))
})

Just to make sure that adding/subtracting were working well. I tried to commit to your fork, but it didn't work.

@klm-turing
Copy link
Contributor Author

klm-turing commented Apr 26, 2024

@willismonroe I added those test for you
Screenshot from 2024-04-26 17-41-41

@willismonroe
Copy link

That looks good!

@klm-turing
Copy link
Contributor Author

klm-turing commented Apr 29, 2024

Hi Dear @willismonroe It's now a plugin to be extended as @iamkun suggested

const date3 = '01-03--2021'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).format('YYYY-MM-DD')).toBe(`-${moment(date).format('YYYY-MM-DD')}`)
expect(dayjs(date).format()).toBe(`-${moment(date).format()}`)
Copy link
Owner

@iamkun iamkun Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we remove moment in this test case? as far as i know, moment does not support nagative year

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamkun Sure

if (typeof date === 'string' && date.indexOf(`-${fullYear}`) !== -1) {
return dayjs(newDate).subtract(fullYear * 2, 'year').toDate()
}
return newDate
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can return the passed in the original cfg.date directly, without dealing with logic like !date

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamkun Got you

@klm-turing
Copy link
Contributor Author

klm-turing commented Apr 30, 2024

@iamkun Changes are done. Let me know if anything else will be great

if (typeof date === 'string' && date.indexOf(`-${fullYear}`) !== -1) {
return dayjs(newDate).subtract(fullYear * 2, 'year').toDate()
}
return newDate
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should return date not newDate, cause cfg.date is the untouched original one, just leave it to the current main parse logic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamkun Updated

@iamkun iamkun merged commit 6a42e0d into iamkun:dev May 12, 2024
4 checks passed
@iamkun
Copy link
Owner

iamkun commented May 12, 2024

THX

@klm-turing klm-turing deleted the feat-negative-year-support branch May 13, 2024 15:35
github-actions bot pushed a commit that referenced this pull request Jul 18, 2024
## [1.11.12](v1.11.11...v1.11.12) (2024-07-18)

### Bug Fixes

* Add NegativeYear Plugin support  ([#2640](#2640)) ([6a42e0d](6a42e0d))
* add UTC support to negativeYear plugin ([#2692](#2692)) ([f3ef705](f3ef705))
* Fix zero offset issue when use tz with locale ([#2532](#2532)) ([d0e6738](d0e6738))
* Improve typing for min/max plugin ([#2573](#2573)) ([4fbe94a](4fbe94a))
* timezone plugin currect parse UTC tz ([#2693](#2693)) ([b575c81](b575c81))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't input negative years
3 participants