...

View Full Version : Java Script To Find Week Day



onlineindex
09-01-2011, 09:07 PM
// since we can't work with the "actual" Date Object we need to create an instance of it (a copy)
// fill the variable now with the current date now is a snap-shot of the Date Information


var now = new Date();

// getDay() returns the day of the week as an integer from 0 (Sunday) to 6 (Saturday).
// by using &&, logical operator, we're checking to see if both parts are true

if (now.getDay() > 0 && now.getDay() < 6) {
document.write("Sorry, it's a weekday.");
}
else {
document.write("Hooray, it's a weekend!");
}

-- OR --

if (now.getDay() > 0 && now.getDay() < 6) document.write("Sorry, it's a weekday.");
else document.write("Hooray, it's a weekend!");

mrhoo
09-01-2011, 09:58 PM
If you check the modulus of the day integer against 6 you can match both 0 and 6 in one statement.

6%6 and 0%6 are both equal to zero.


var weekendstring= new Date().getDay()%6 ?
"Sorry, it's a weekday.":"Hooray, it's a weekend!";

document.write(weekendstring)

Old Pedant
09-01-2011, 10:32 PM
Nice trick, MrHoo.

Now the real question: Why the heck did "index" post that in the first place?

On top of which, his comment at the top is *WRONG*.


// since we can't work with the "actual" Date Object we need to create an instance of it (a copy)

NONSENSE!

MrHoo's code proved otherwise. There's no "copy" of an "actual" Date object in his code, at all!

And even if you don't use his trick:


if ( (new Date()).getDay() > 0 && (new Date()).getDay() < 6) {
document.write("Sorry, it's a weekday.");
}
else {
document.write("Hooray, it's a weekend!");
}

Granted, it may make more sense to assign it to a variable if you use that code, but "can't work with" is clearly not true.

And besides, if you are going to use that code, it would make more sense to do:


var day = (new Date()).getDay();
if (day > 0 && day < 6) {
document.write("Sorry, it's a weekday.");
}
else {
document.write("Hooray, it's a weekend!");
}

Philip M
09-02-2011, 08:38 AM
I have the idea that
// since we can't work with the "actual" Date Object we need to create an instance of it (a copy)
is supposed to mean that you cannot use the information in the Date Object (say getTime()) without creating an instance of it as var now = New Date(). Hence now is a snap-shot of the Date Information. Not well phrased and a bit obvious.

Old Pedant
09-02-2011, 09:08 AM
And here I thought *I* was the pedant!

Yes, I suppose that, at some level, all that's true.

In actuality, there is no such thing as a "Date object" until you create one. There is only a Date constructor (the constructor is a function, and in JS even functions are objects, but let's not travel that slippery path). So indeed you must do new Date() to invoke the constructor to create the object that we can *then* call "an instance of a Date object."

This all comes about because JavaScript refused to entertain the notion of "classes" and insisted on calling everything objects. In any other language, we'd say there really *IS* no such thing as a Date object. There would be a Date class, and then you could create an object REFERENCE that refers to an instance of the Date class, which is an object (but not, technically, a Date object).

But JS decided to lump everything under one name. Ugh. A sloppy language. It's fun, but it's still sloppy.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum