/    Sign up×
Community /Pin to ProfileBookmark

JS calculations correct in IE wrong in Moz

I have a calendar entry page that calculates the day of the week for the date entered based on a function.

Internet explorer gets the days right but Moz outputs the days two off from what they really are.

The date is entered like 20040331, the code snippet below processes the form value

strDate = document.eventadd.FromDate.value;
strYear = strDate.substring(0,4);

strMonth = strDate.substring(4,6);

strDay = strDate.substring(6,8);

var months = new makeArray(‘Jan.’,’Feb.’,’Mar.’,’Apr.’,’May’,’Jun.’,’Jul.’,’Aug.’,’Sep.’,’Oct.’,’Nov.’,’Dec.’);
var daysofweek = new makeArray(‘Sunday’,’Monday’,’Tuesday’,’Wednesday’,’Thursday’,’Friday’,’Saturday’);
var jMonth = parseInt(strMonth);
jMonth=jMonth-1;
var date = new Date(strYear,jMonth,strDay);

var day = date.getDate();
var year = date.getYear();
var month = date.getMonth() + 1;

I believe that this function the piece that is off

function DayOfWeek(day,month,year) {
var a = Math.floor((14 – month)/12);
var y = year – a;
var m = month + 12*a – 2;
var d = (day + y + Math.floor(y/4) – Math.floor(y/100) +
Math.floor(y/400) + Math.floor((31*
m)/12)) % 7;
return d + 1;
}

Here is the url where the page code is active.

http://www.theledger.com/apps/pbcs.dll/eventsadd?Category=CALENDAR

Thanks

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JuuitchanMar 31.2004 — I do not understand your style of coding.

First of all, getYear() sometimes returns bad values. Try this code:

var myDate=new Date("January 1, 2005");

document.write(myDate.getYear());

Try it in Netscape *and* in IE.

If you want the correct Gregorian year (for example, 2005 for dates in AD 2005), use getFullYear().

Note: Some older browsers do not have getFullYear(). If I were you, I would check to see if getYear() is less than 300, and if it is, add 1900.

JavaScript will calculate days of the week for you with its getDay() function, if I remember correctly. It returns a number from 0 to 6. I'll have to check to see which number equals which day, since I'm not sure.
Copy linkTweet thisAlerts:
@satelite21authorMar 31.2004 — [i]Originally posted by Juuitchan [/i]

If you want the correct Gregorian year (for example, 2005 for dates in AD 2005), use getFullYear().
[/QUOTE]


Thanks Juuitchan changing the code from getYear to get FullYear made it work.
Copy linkTweet thisAlerts:
@JuuitchanApr 01.2004 — Did you do date validation (like if the user types in a garbage date, or a date in the wrong format)?

Also: You can convert strings to numbers. The minus sign (for subtraction) converts everything to a number, so just try subtracting zero. But I wouldn't rely on this too much.

There is also a parseInt() function. The correct way to use parseInt() is this:

var boing="42";

var numboing=parseInt(boing, 10);

The parseInt function will take the numeric value of boing. The "Int" in "parseInt" means integer (whole number), so don't try this on decimals. The "10" is because we want to read boing as a base 10 number.
×

Success!

Help @satelite21 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...