Skip to content

Commit

Permalink
babakhani#53 fix add/subtract weeks
Browse files Browse the repository at this point in the history
  • Loading branch information
mort3za committed Mar 22, 2019
1 parent d328b88 commit 6652674
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
44 changes: 28 additions & 16 deletions dist/persian-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ var Helpers = function () {
unit = 'year';
} else if (durationUnit.month.indexOf(unit) > -1) {
unit = 'month';
} else if (durationUnit.week.indexOf(unit) > -1) {
unit = 'week';
} else if (durationUnit.day.indexOf(unit) > -1) {
unit = 'day';
} else if (durationUnit.hour.indexOf(unit) > -1) {
Expand Down Expand Up @@ -1537,22 +1539,27 @@ var PersianDateClass = function () {
this.month(newMonth);
}
}
if (unit === 'day') {
if (unit === 'week') {
var oldHour = this.hour();
var newDate = this.valueOf() + value * 24 * 60 * 60 * 1000;
var newDate = this.valueOf() + value * 7 * 24 * 60 * 60 * 1000;
return this.unix(newDate / 1000).hour(oldHour);
}
if (unit === 'day') {
var _oldHour = this.hour();
var _newDate = this.valueOf() + value * 24 * 60 * 60 * 1000;
return this.unix(_newDate / 1000).hour(_oldHour);
}
if (unit === 'hour') {
var _newDate = this.valueOf() + value * 60 * 60 * 1000;
return this.unix(_newDate / 1000);
var _newDate2 = this.valueOf() + value * 60 * 60 * 1000;
return this.unix(_newDate2 / 1000);
}
if (unit === 'minute') {
var _newDate2 = this.valueOf() + value * 60 * 1000;
return this.unix(_newDate2 / 1000);
var _newDate3 = this.valueOf() + value * 60 * 1000;
return this.unix(_newDate3 / 1000);
}
if (unit === 'second') {
var _newDate3 = this.valueOf() + value * 1000;
return this.unix(_newDate3 / 1000);
var _newDate4 = this.valueOf() + value * 1000;
return this.unix(_newDate4 / 1000);
}
if (unit === 'millisecond') {
// log('add millisecond')
Expand Down Expand Up @@ -1591,22 +1598,27 @@ var PersianDateClass = function () {
this.date(oldDate);
}
}
if (unit === 'day') {
if (unit === 'week') {
var oldHour = this.hour();
var newDate = this.valueOf() - value * 24 * 60 * 60 * 1000;
var newDate = this.valueOf() - value * 7 * 24 * 60 * 60 * 1000;
return this.unix(newDate / 1000).hour(oldHour);
}
if (unit === 'day') {
var _oldHour2 = this.hour();
var _newDate5 = this.valueOf() - value * 24 * 60 * 60 * 1000;
return this.unix(_newDate5 / 1000).hour(_oldHour2);
}
if (unit === 'hour') {
var _newDate4 = this.valueOf() - value * 60 * 60 * 1000;
return this.unix(_newDate4 / 1000);
var _newDate6 = this.valueOf() - value * 60 * 60 * 1000;
return this.unix(_newDate6 / 1000);
}
if (unit === 'minute') {
var _newDate5 = this.valueOf() - value * 60 * 1000;
return this.unix(_newDate5 / 1000);
var _newDate7 = this.valueOf() - value * 60 * 1000;
return this.unix(_newDate7 / 1000);
}
if (unit === 'second') {
var _newDate6 = this.valueOf() - value * 1000;
return this.unix(_newDate6 / 1000);
var _newDate8 = this.valueOf() - value * 1000;
return this.unix(_newDate8 / 1000);
}
if (unit === 'millisecond') {
// log('add millisecond')
Expand Down
2 changes: 1 addition & 1 deletion dist/persian-date.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class Helpers {
else if (durationUnit.month.indexOf(unit) > -1) {
unit = 'month';
}
else if (durationUnit.week.indexOf(unit) > -1) {
unit = 'week';
}
else if (durationUnit.day.indexOf(unit) > -1) {
unit = 'day';
}
Expand Down
10 changes: 10 additions & 0 deletions src/pDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,11 @@ class PersianDateClass {
this.month(newMonth);
}
}
if (unit === 'week') {
const oldHour = this.hour();
let newDate = this.valueOf() + (value * 7 * 24 * 60 * 60 * 1000);
return this.unix(newDate / 1000).hour(oldHour);
}
if (unit === 'day') {
const oldHour = this.hour();
let newDate = this.valueOf() + (value * 24 * 60 * 60 * 1000);
Expand Down Expand Up @@ -1294,6 +1299,11 @@ class PersianDateClass {
this.date(oldDate);
}
}
if (unit === 'week') {
const oldHour = this.hour();
let newDate = this.valueOf() - (value * 7 * 24 * 60 * 60 * 1000);
return this.unix(newDate / 1000).hour(oldHour);
}
if (unit === 'day') {
const oldHour = this.hour();
let newDate = this.valueOf() - (value * 24 * 60 * 60 * 1000);
Expand Down

0 comments on commit 6652674

Please sign in to comment.