Why date set as variable is changing by jQuery datepicker?

I am using jquery-ui for making a vaccine schedule on user input.

Below is the Schedule of the vaccine:

Vaccine Schedule

here, d=day, m=month, y=year.

Below is the Schedule Form from where the date of above schedule can be made: Schedule Form

You can see, if a user set 01/09/2015 as Date of Birth, then 1 day after Birth is 02/09/2015 for Anti Rabies Vaccine - 1st Dose

On the other hand, for the same Date of Birth , 10 year after birth should be 01/09/2015 which is Cervical Cancer - 1st Dose. But my schedule form is giving 1st Dose as 02/09/2015

This is also true for Hepatitis-A (Adult) - 1st Dose.

So, instead calculating from Date of Birth, 1st Dose of Cervical Cancer is calculating from 1st Dose of Anti Rabies Vaccine and 1st Dose of Hepatitis-A (Adult) Vaccine is calculating from 1st Dose of Cervical Cancer Vaccine instead calculating from Date of Birth.

This is happening because in my jQuery code, I declared selected date of birth as a variable var date2. This date2 is changing at the later part of code while I am using an equation var date1=date2 which should not be. I want to tell the jQuery engine that let date1 is date2, but it is altering the value of date2 itself. If this can be fixed, i.e value of date2 can be kept constant, then the jQuery code will work as I expected. But I can't figure it out why value of date2 is changing to value of date1 by the equation var date1=date2.

Instead of mentioning the full jQuery code here, I am giving a jsfiddle in which all HTML, CSS as well as the jQuery code are shown.

Please follow this link for testing purpose:

http://jsfiddle.net/infomamun/82c7s3te/

Upvotes: 0

Views: 88

Answers (1)

Scimonster
Scimonster

Reputation: 33399

It's because you set the date based on the previous ones. See your last functional line:

$(this).datepicker('setDate', date1);

After the first row, date1 is 1 day after the birthday. That throws off the calculations for the rest of the chart.

To fix it, i would recommend storing the date using jQuery's .data() method instead, and resetting it to the birthday at the end of each row.

But is it really a problem? After all, who wants to go get those shots on their birthday? :)

Upvotes: 1

Related Questions