/    Sign up×
Community /Pin to ProfileBookmark

Newbie help with a function

hi,

i have been given a function to add working days to a date. here it is:

[CODE]function addWorkingDays(myDate,days) { //myDate = starting
date, days = no. working days to add.
var temp_date = new Date();
var i = 0;
var days_to_add = 1;
while (i < (days)){
temp_date = new Date(myDate.getTime() +
(days_to_add*24*60*60*1000));
//0 = Sunday, 6 = Saturday, if the date not
equals a weekend day then increase by 1
if ((temp_date.getDay() != 0) &&
(temp_date.getDay() != 6)){
i+=1;
}
days_to_add += 1;
}
return new Date(myDate.getTime() +
days_to_add*24*60*60*1000);
}
[/CODE]

i understand this has to go into the head section of my page but need a little help displaying it on my website. what i want it to do is add x number of days to todays date. im pretty sure it was designed to add working days to a user submitted day but it cant be that hard to have it calculate based on todays date. also is there a way of makeing it uk compatible eg swapping the day and month (dd/mm/yyy instead of mm/dd/yyy).

thanks very much in advance

chris

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@toicontienJan 23.2009 — Ensure the script you place on your page does not have the same line breaks, as inline comments are broken into two lines in the code you posted, and will cause syntax errors:
[code=php]function addWorkingDays(myDate,days) {
//myDate = starting date, days = no. working days to add.
var temp_date = new Date();
var i = 0;
var days_to_add = 1;
while (i < (days)){
temp_date = new Date(myDate.getTime() + (days_to_add*24*60*60*1000));
//0 = Sunday, 6 = Saturday, if the date not equals a weekend day then increase by 1
if ((temp_date.getDay() != 0) && (temp_date.getDay() != 6)){
i+=1;
}
days_to_add += 1;
}
return new Date(myDate.getTime() + days_to_add*24*60*60*1000);
}[/code]

You are correct in putting this function in the <head>. Now to use this function:
[code=html]<script type="text/javascript">
<!--
document.writeln( addWorkingDays(new Date(), 5) );
// -->
</script>[/code]
Copy linkTweet thisAlerts:
@chris2403authorJan 23.2009 — Thats great, thanks

one more thing, is there any way to strip a few things off the result.

eg

when i get:

Mon Feb 02 2009 17:55:21 GMT+0000 (GMT Standard Time)

Can i get:

02 February 2009
Copy linkTweet thisAlerts:
@skywalker2208Jan 23.2009 — You can use different date methods to convert the date to the format you want.

http://www.w3schools.com/jsref/jsref_obj_date.asp
Copy linkTweet thisAlerts:
@chris2403authorJan 23.2009 — ok, this is what i have so far:

[code=php]<html>
<head>
<script type="text/javascript">
function addWorkingDays(myDate,days) {
//myDate = starting date, days = no. working days to add.
var temp_date = new Date();
var i = 0;
var days_to_add = 1;
while (i < (days)){
temp_date = new Date(myDate.getTime() + (days_to_add*24*60*60*1000));
//0 = Sunday, 6 = Saturday, if the date not equals a weekend day then increase by 1
if ((temp_date.getDay() != 5) && (temp_date.getDay() != 6)){
i+=1;
}
days_to_add += 1;
}
return new Date(myDate.getTime() + days_to_add*24*60*60*1000);
}
var date_display = addWorkingDays(new Date(), 11);
var day_display = getDate(date_display);
</script>
</head>
<body>
<script type="text/javascript">
<!--
document.writeln(day_display);
// -->
</script>
</body>
</html>
[/code]


i must be missing something because i get "undefined" as a result. when i change [code=php]document.writeln(day_display);[/code] to [code=php]document.writeln(date_display);[/code] i get the full date as above so the problem must be with the variable day_display or after ?
Copy linkTweet thisAlerts:
@toicontienJan 23.2009 — Where do you have a getDate() function defined?
var day_display = [B]getDate(date_display)[/B];
Copy linkTweet thisAlerts:
@chris2403authorJan 23.2009 — i probably dont, ive sorted the problem now though i think.

cheers for your help
×

Success!

Help @chris2403 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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