Weird Datepicker issue

joshuajohnhutchison
joshuajohnhutchison used Ask the Experts™
on
Ok, so I noticed something weird happening with my datepicker... By selecting the date in one field, it limits the date you can choose in the other field.   It normally works perfect but notice what happens when you select any date in February.  Once you select the date in either field in February, when you select the other field it is reading February as March!?!  Whats wierder is that it just started doing this.  In other words, last week when you selected February and then selected the other field it would read as February... and the script has not been changed.  

Anyway, I attached a sample file so you can see what I'm talking about.  "Terry_focus" created this script for me on this site a while ago because I'm new to jQuery.  I don't know what the "10" parameter in the datePart is for but I'm thinking perhaps something is happening right there.  Anyway, Terry_foucs... where you at dude?  Hook it up again and let me know what you think
datepicker-test.html
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Actually, come to think of it, it worked fine yesterday.  So I am thinking the problem lies somewhere between the current date, number of days in February and the datePart line in the code that has to do with the month.  I don't know though... I'm like a child who wonders into the middle of a movie and wants to know whats going on... haha  100 extra points for the person who knows what movie thats from.

Commented:
The problem appears to be with your date logic.

I included http://www.javascripttoolbox.com/lib/date/source.php this date library
Then I replaced your date parsing logic with
                var d = new Date(this.value);

Everything is working fine now.
Michel PlungjanIT Expert
Top Expert 2009

Commented:
Please explain what is wrong with

var datePart = this.value.split("-");
                var d = new Date();
                d.setYear(datePart[0]);
                d.setMonth(parseInt(datePart[1],10)-1);//JavaScript is month -1
                d.setDate(datePart[2]);

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
I'm not sure what's wrong with it, but when I used your date logic, it didn't work.  When I used the other library to build the date it did work.  The error must be in your date logic.
Michel PlungjanIT Expert
Top Expert 2009

Commented:
Ahh

I know
javascript:var datePart = "10-02-28".split("-");var d = new Date();d.setYear(datePart[0]);d.setMonth(parseInt(datePart[1],10)-1);d.setDate(datePart[2]);alert(d)

gives a date in 1910

you need to use setFullYear(2000+parseInt(datePart[0],10));

See:
javascript:var datePart = "10-02-28".split("-");var d = new  Date();d.setFullYear(2000+parseInt(datePart[0],10));d.setMonth(parseInt(datePart[1],10)-1);d.setDate(datePart[2]);alert(d)

Michel PlungjanIT Expert
Top Expert 2009

Commented:
so change
      d.setYear(datePart[0]);
to
     d.setFullYear(2000+parseInt(datePart[0],10));

instead of using some huge date script.

Author

Commented:
mplungjan,

I replaced the code as you stated above and now its doing the same thing except my year is 4010.  
Michel PlungjanIT Expert
Top Expert 2009

Commented:
Then you are NOT getting yy-mm-dd back. Remove the 2000+ from my code

Author

Commented:
Nah man, that wasn't the trick.  It still doesn't work.  I haven't yet tried SRigney's solution as I would rather not have to use another script so I'm holding off on that to see if you can figure it out.  I'm going to be busy today so if you're working, just use the test file i originally attached to test any new epiphanies. Later dude. Thanks for the help.
IT Expert
Top Expert 2009
Commented:
Here
$(function() {
		$('#start_date').datepicker({
			minDate: 0,		
			dateFormat: 'yy-mm-dd',
			numberOfMonths: 3,
			showButtonPanel: false,			
		}).change(function(){
                //Break open your dates to create a date object
                var datePart = this.value.split("-");
                var d = new Date(datePart[0],datePart[1]-1,datePart[2],0,0,0)
                //Use getter an setters to adjust end date picker
                $('#end_date').datepicker('option', 'minDate',d);
        });
		$('#end_date').datepicker({
			minDate: 0,
			dateFormat: 'yy-mm-dd',
			numberOfMonths: 3,
			showButtonPanel: false,
		}).change(function(){
                var datePart = this.value.split("-");
                var d = new Date(datePart[0],datePart[1]-1,datePart[2],0,0,0)
                //Use getter an setters to adjust end date picker
                $('#start_date').datepicker('option', 'maxDate',d);
        });
	}); 

Open in new window

Author

Commented:
Ok, so as I was thinking this might happen, today is the first of February and the problem vanished.  Everything works perfect again and I did not make any changes to the script.  So the issue definitely had something to do with the current date.  I guess the new problem is, since I was busy yesterday and didn't have a chance to test either solution and now can't, I don't know what to do in terms of selecting an accepted solution to award points.  Any ideas?...
Michel PlungjanIT Expert
Top Expert 2009

Commented:
Change the date on your PC so you can test it.
My LAST script change definitely made the script work so please accept my last suggestion as answer

Thanks

Author

Commented:
its all good man, I don't care that much.  If it happens again, now I know where to come for the answer.  Thanks for the help.  SRigney, you too dude.  Thanks for taking the time.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial