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 be07931 commit 7790be7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
48 changes: 30 additions & 18 deletions dist/persian-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ return /******/ (function(modules) { // webpackBootstrap
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
Expand Down 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 @@ -1540,22 +1542,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 @@ -1594,22 +1601,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 @@ -1245,6 +1245,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 @@ -1297,6 +1302,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
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,16 @@ describe('Subtract', function () {
assert.deepEqual(a, [1392, 12, 29, 1, 1, 1, 0]);
});

it('Weeks', function () {
let a = new pDate([1397, 1, 1, 1, 1, 1]).add('week', 1).toArray();
assert.deepEqual(a, [1397, 1, 8, 1, 1, 1, 0]);
});

it('Weeks', function () {
let a = new pDate([1397, 1, 8, 1, 1, 1]).subtract('week', 1).toArray();
assert.deepEqual(a, [1397, 1, 1, 1, 1, 1, 0]);
});

it('Days Base on month day count', function () {
let a = new pDate([1395, 6, 31, 1, 1, 1]).subtract('day', 30).toArray();
assert.deepEqual(a, [1395, 6, 1, 1, 1, 1, 0]);
Expand Down

0 comments on commit 7790be7

Please sign in to comment.