Skip to content

Commit

Permalink
fix daylight saving time close#262 close#329
Browse files Browse the repository at this point in the history
* fix(dayjs.add): fix daylight saving time

It's not safe to add 24 hours to manipulate date, There are 2 days of the year that do not have 24 hours
In order to get rid of this issue its good to use build-in `setDate`
See: https://en.wikipedia.org/wiki/Daylight_saving_time
  • Loading branch information
farnabaz authored and iamkun committed Sep 26, 2018
1 parent d0b1419 commit 969aced
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,23 @@ class Dayjs {
const date = this.set(C.DATE, 1).set(u, n + number)
return date.set(C.DATE, Math.min(this.$D, date.daysInMonth()))
}
const instanceFactorySet = (n) => {
const date = new Date(this.$d)
date.setDate(date.getDate() + (n * number))
return wrapper(date, this)
}
if (unit === C.M) {
return instanceFactory(C.M, this.$M)
}
if (unit === C.Y) {
return instanceFactory(C.Y, this.$y)
}
if (unit === C.D) {
return instanceFactorySet(1)
}
if (unit === C.W) {
return instanceFactorySet(7)
}
let step
switch (unit) {
case C.MIN:
Expand All @@ -252,12 +263,6 @@ class Dayjs {
case C.H:
step = C.MILLISECONDS_A_HOUR
break
case C.D:
step = C.MILLISECONDS_A_DAY
break
case C.W:
step = C.MILLISECONDS_A_WEEK
break
case C.S:
step = C.MILLISECONDS_A_SECOND
break
Expand Down

0 comments on commit 969aced

Please sign in to comment.