Skip to content

Commit

Permalink
Add toJalaali conversion from Date objects
Browse files Browse the repository at this point in the history
  • Loading branch information
behrang committed Aug 3, 2016
1 parent ba66249 commit 7079111
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ Converts a Gregorian date to Jalaali.
jalaali.toJalaali(2016, 4, 11) // { jy: 1395, jm: 1, jd: 23 }
```

### toJalaali(date)

Converts a JavaScript Date object to Jalaali.

```js
jalaali.toJalaali(new Date(2016, 3, 11)) // { jy: 1395, jm: 1, jd: 23 }
```

### toGregorian(jy, jm, jd)

Converts a Jalaali date to Gregorian.
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ module.exports =
Converts a Gregorian date to Jalaali.
*/
function toJalaali(gy, gm, gd) {
if (Object.prototype.toString.call(gy) === '[object Date]') {
gd = gy.getDate()
gm = gy.getMonth() + 1
gy = gy.getFullYear()
}
return d2j(g2d(gy, gm, gd))
}

Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
},
"homepage": "https://github.com/jalaali/jalaali-js",
"devDependencies": {
"component": "^1.0.0-rc5",
"mocha": "^1.21.3",
"should": "^4.0.4"
"mocha": "^3.0.0",
"should": "^10.0.0"
}
}
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ describe('toJalaali', function () {
j.toJalaali(2013, 1, 10).should.be.eql({jy: 1391, jm: 10, jd: 21})
j.toJalaali(2014, 8, 4).should.be.eql({jy: 1393, jm: 5, jd: 13})
})

it('should convert Date object to Jalaali', function () {
j.toJalaali(new Date(1981, 8 - 1, 17)).should.be.eql({jy: 1360, jm: 5, jd: 26})
j.toJalaali(new Date(2013, 1 - 1, 10)).should.be.eql({jy: 1391, jm: 10, jd: 21})
j.toJalaali(new Date(2014, 8 - 1, 4)).should.be.eql({jy: 1393, jm: 5, jd: 13})
})
})

describe('toGregorian', function () {
Expand Down

0 comments on commit 7079111

Please sign in to comment.