Apologies - getDate() over-ride to return current or last workday

B

Bill Edwards

Sorry for forgetting to post my code yesterday, the bit that works is
similar to something I last in here last week

var now = new Date();
var dayNames = new
Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var monNames = new
Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
alert("Today is: " + dayNames[now.getDay()] + " " +
monNames[now.getMonth()] + " " + now.getDate());

I saw the below code on a site for working out workdays (Mon thru Fri)
but I can't get it to work with the above, some expected ";" error.

function weekend();
{
startDate = new Date();
if(startDate.getDay() == 7){
startDate.setDate( startDate.getDate() - 1);
}
else {
if(startDate.getDay() == 1){
startDate.setDate( startDate.getDate() - 2);
}
}
return startDate;
}

I'd like all Saturdays and Sundays to return Friday's date so that I
can change the alert text to "The current or last workday is:" Is this
possible? I'm making this far too complicated aren't I?
 
M

Michael Winter

On Mon, 13 Sep 2004 22:34:09 +0100, Bill Edwards

[snip]
var dayNames = new
Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var monNames = new
Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

[snip]

It's usually considered better to initialise an array with array literal
notation:

var dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
monNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

As for getting weekdays, use something like:

function toWeekday(d) {
// Rotate the weekday indicies so that Sat = 0, Sun = 1, etc.
var o = (d.getDay() + 1) % 7,
// Make a copy of the supplied date
t = new Date(d);
// If Saturday or Sunday, change to Friday
if(o < 2) {t.setDate(t.getDate() - (o + 1));}
// Return the new date
return t;
}

Pass in a Date object and get a modified object as the return value.

[snip]

Hope that helps,
Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Mon, 13 Sep 2004 22:34:09, seen in Bill
Edwards <[email protected]> posted :

When continuing a topic, do not start a new thread. Read the newsgroup
FAQ.
function weekend();
{
startDate = new Date();
if(startDate.getDay() == 7){
startDate.setDate( startDate.getDate() - 1);
}
else {
if(startDate.getDay() == 1){
startDate.setDate( startDate.getDate() - 2);
}
}
return startDate;
}

The days of the week, in javascript, are 0 to 6.

Never believe script off the Web until you have tested each step in it
yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top