jalaali / jalaali-js

JavaScript functions for converting Jalaali and Gregorian calendar systems to each other
MIT License
310 stars 36 forks source link

toGregorian problem? #2

Closed Mds92 closed 9 years ago

Mds92 commented 9 years ago

I've tested your library. I think there is a problem with it. Assume I wanna convert 1394/07/01 to Gregorian; So I wrote the following code:

var gregorianDate = toGregorian(1394, 07, 01),
    date = new Date(gregorianDate.gy, gregorianDate.gm, gregorianDate.gd);

after converting date shows Fri Oct 23 2015 00:00:00 GMT+0330 (Iran Standard Time) whiles the first day of Mehr in 1394 is Wed Sep 23 2015 00:00:00 GMT+0330 (Iran Standard Time)

behrang commented 9 years ago

Please note that in this library, months are 1-based. That is, the first month is month number 1, not 0. But in the Date object in JavaScript, months are 0-based. So you have to consider that difference when working with both of them.

To fix the problem you have to do something like this:

var gregorianDate = toGregorian(1394, 07, 01),
    date = new Date(gregorianDate.gy, gregorianDate.gm - 1, gregorianDate.gd);
Mds92 commented 9 years ago

OK, but I think it's better to return zero base for gregorianDate. Thanks anyway.

danielstocks commented 8 years ago

Hey,

Thanks so much for this library.

I'm curious as to why you decided to go with 1-indexed based months, rather than 0-index? Is it for purely aesthetics or are there technical reasons?

It makes it very cumbersome to work with when converting dates back and forth.

behrang commented 8 years ago

One reason is I had some problems with 0-based months myself, while working with JS date object. Another is it seems odd to me that months start with 0 but days start with 1. Also I think the algorithm that is implemented, was 1-based and I tried to keep it like that.

danielstocks commented 8 years ago

@behrang Ok, makes sense. thank you

Ps. JavaScript Date is very odd :)