PDA

View Full Version : November dates wrong



Princeton Phil
10-31-2016, 05:48 PM
1) Script Title: Dynamic Date Selector (DtTvB)

2) Script URL: http://www.javascriptkit.com/script/script2/dyndateselector.shtml

3) Describe problem: Has worked fine but I just noticed that there's a problem with getting the November calendar dates. The month shows but the calendar dates are those of December. You can see it on the Example.

styxlawyer
11-01-2016, 12:03 AM
The example page looks correct to me. 1st November 2016 is a Tuesday.

Princeton Phil
11-01-2016, 12:33 AM
That's not what I see. I see November 1 as a Thursday.

jscheuer1
11-01-2016, 03:04 AM
I see the problem Thursday is 11/1/16 in IE 11, Opera, Chrome, and Firefox (all latest for Win 7). What do you think could be causing you (styxlawyer) to see the correct Tuesday for 11/1/16? Maybe you're in a different timezone and it wasn't Oct 31 yet, or was already Nov 1. In any case, I will be looking at the code and report back what I find, if anything.

jscheuer1
11-01-2016, 03:36 AM
OK, in limited testing it appears the problem is that the date is set after the month and year, which apparently allows it to sometimes go ahead an extra month owing to the fact that some months are shorter than others and on the last day of a long month that pushes it ahead to the first day of the next month after next. I've changed it to set the date prior to setting the the month and year, hopefully this will not allow it to jump backward one extra month in certain cases - I'm thinking it probably will not. In any case, change:


// This one draws calendar...
function ds_draw_calendar(m, y) {
// First clean the output buffer.
ds_ob_clean();
// Here we go, do the header
ds_echo (ds_template_main_above(ds_monthnames[m - 1] + ' ' + y));
for (i = 0; i < 7; i ++) {
ds_echo (ds_template_day_row(ds_daynames[i]));
}
// Make a date object.
var ds_dc_date = new Date();
ds_dc_date.setMonth(m - 1);
ds_dc_date.setFullYear(y);
ds_dc_date.setDate(1);
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
days = 31;
} else if (m == 4 || m = . . .

To:


// This one draws calendar...
function ds_draw_calendar(m, y) {
// First clean the output buffer.
ds_ob_clean();
// Here we go, do the header
ds_echo (ds_template_main_above(ds_monthnames[m - 1] + ' ' + y));
for (i = 0; i < 7; i ++) {
ds_echo (ds_template_day_row(ds_daynames[i]));
}
// Make a date object.
var ds_dc_date = new Date();
ds_dc_date.setDate(1);
ds_dc_date.setMonth(m - 1);
ds_dc_date.setFullYear(y);
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
days = 31;
} else if (m == 4 || m = . . .

styxlawyer
11-01-2016, 08:11 AM
Sorry for not replying earlier, I was asleep! I'm in the UK on GMT using Windows 8.1 with Opera 41.0 and it was already 1st November when I looked at the demo page.

Princeton Phil
11-01-2016, 03:22 PM
Thanks for concurring with my observation. I love the script -- I'm using it on one of my websites and was surprised when it misbehaved. I'm not familiar with javascript so I appreciate your effort to debug it.

jscheuer1
11-01-2016, 04:18 PM
Did you see my fix?

http://www.dynamicdrive.com/forums/showthread.php?80601-November-dates-wrong&p=319551#post319551

Princeton Phil
11-02-2016, 04:33 PM
Yes I see your fix. I'll patch it on my website.
Can you make the change on the Javascriptkiit website so others won't be trapped in the future.

jscheuer1
11-02-2016, 10:46 PM
I'm neither a contributor to nor admin of javascriptkit. So my voice would be as likely to gain notice as anyone's on this issue. I will look into reporting it.

Oh, looks like the admin from here runs that site as well (something I often suspected, but was never sure of). So that increases the chances it will be dealt with. In any case, I sent him an email explaining the issue and the fix.