/    Sign up×
Community /Pin to ProfileBookmark

excluding weekends from a date script

Hi,

I currently have a estimated date script i run to give users of my site a estimated delivery date for a product. This all works fine and looks this in the body:

<span class=”estdeliverydateoutput”><script>

//create the date
var myDate = new Date();

//add a day to the date
myDate.setDate(myDate.getDate() + 6);

document.write(myDate.format(‘D jS, M’));

However i need the code to exclude weekends, as it is not possible to deliver on a weekend, how would i go about changing the script so that it adds 2 or 1 days to a weekend so that the next delivery day is a monday for example.

Thanks

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@mityaJun 22.2009 — JS isn't best suited to what you're doing. Handling orders, shopping carts, delivery dates is much more of a server-side thing.

Also, remember JS reads the client's, not server's, date and time - so be careful with date/time functions with JS.

In PHP you can easily find out the day of a given date by mixing the date() and mktime() functions.

www.php.net/date

www.php.net/mktime (particularly this one)

Consider this.

Cheers
Copy linkTweet thisAlerts:
@twoseasonsauthorJun 22.2009 — The code would not be linked to the cart or anything like that. It would be a stand alone from the ordering system which just sits on the page.
Copy linkTweet thisAlerts:
@mityaJun 22.2009 — Nonetheless the problems remain, i.e. JS is not suited to date handling and PHP can sort this for you in one line of code.

Otherwise you're headed for a very messy function to find out what day a particular date is.
Copy linkTweet thisAlerts:
@twoseasonsauthorJun 22.2009 — I understand that. How ever unforntunetly i am not able to run PHP on the site as it is made in coldfusion and i am not able to access it, as it is run my another company. ?
Copy linkTweet thisAlerts:
@astupidnameJun 22.2009 — [CODE]<script type="text/javascript">

var myDate = new Date();
myDate.setDate(myDate.getDate() + 6); //this actually adds 6 days on, is that what you intended?
//you say one day in your code?
alert(myDate);
if (/sat/i.test(myDate)) {
myDate.setDate(myDate.getDate() + 2);
} else if (/sun/i.test(myDate)) {
myDate.setDate(myDate.getDate() + 1);
}

alert(myDate);

</script>[/CODE]
Copy linkTweet thisAlerts:
@twoseasonsauthorJun 22.2009 — Hi,

Thanks, yes i need it to add 6 days for delivery but exclude saturdays and sundays changing it to the next possible day which would be Monday...
Copy linkTweet thisAlerts:
@twoseasonsauthorJun 22.2009 — It seems to pop up an error message? Is there a way of it just changing the out put on the actual page? Thanks for your help! ?
Copy linkTweet thisAlerts:
@astupidnameJun 22.2009 — It seems to pop up an error message? Is there a way of it just changing the out put on the actual page? Thanks for your help! ?[/QUOTE]
I don't get any errors from the code I posted in IE or FF, don't know what error are you getting exactly? And how exactly are you implementing? Just with the alerts or what?

You could also do it like the following, would probably be better (earlier, I was just sorta in a regex type of mood..):
[CODE]<script type="text/javascript">

var myDate = new Date();
myDate.setDate(myDate.getDate() + 6);
alert(myDate);
var day = myDate.getDay(); //getDay() returns 0-6, 0 for sunday..
if (day == 6) {
myDate.setDate(myDate.getDate() + 2);
} else if (day == 0) {
myDate.setDate(myDate.getDate() + 1);
}

alert(myDate);

</script>[/CODE]
Copy linkTweet thisAlerts:
@twoseasonsauthorJun 22.2009 — I replace the code i had with the code you provided. These messages poped up. I guess i have to add a part to write the date on the page instead of a pop up message? Javascript is fairly new to me so please forgive me if im being a little blind! :p

[upl-file uuid=fd32d8dd-90e1-4f57-aa46-d071ff77f1d4 size=60kB]Untitled-1.jpg[/upl-file]
Copy linkTweet thisAlerts:
@astupidnameJun 22.2009 —  Untitled-1.jpg (60.3 KB, 0 views) [/QUOTE]
Lol, forgive me but that's rather funny. I assumed you would know those were test alerts which we normally use for example purposes. Based on your first post I guess I should have just used document.write like you had shown, so I guess the following is what you want, presuming that there is actually a Date.prototype.format function defined somewheres within your code which is not shown:

[CODE]<script type="text/javascript">

var myDate = new Date();
myDate.setDate(myDate.getDate() + 6);
var day = myDate.getDay(); //getDay() returns 0-6, 0 for sunday..
if (day == 6) {
myDate.setDate(myDate.getDate() + 2);
} else if (day == 0) {
myDate.setDate(myDate.getDate() + 1);
}

document.write(myDate.format('D jS, M'));

</script>[/CODE]
Copy linkTweet thisAlerts:
@twoseasonsauthorJun 22.2009 — Ha ok, my bad - but i only started looking at javascript last thursday ?

So i have the Date.prototype.format function in the actual javascript file, but not in the body of the page....

thanks
Copy linkTweet thisAlerts:
@astupidnameJun 22.2009 — Ha ok, my bad - but i only started looking at javascript last thursday [/QUOTE]
Yah, that's fine, I didn't mean to belittle or anything, we all start somewhere so I wasn't actually laughing at you per se, just amused (I was there once too you know...).

Your welcome, and happy trails to you! ?
Copy linkTweet thisAlerts:
@krishkilaparthi323Feb 16.2021 — the above logic is implemented for saturday / sunday's , incase if we need to include client holidays, How can we do that. Any idea, can you please help on it.
Copy linkTweet thisAlerts:
@NogDogFeb 16.2021 — @krishkilaparthi323#1628054 Instead of replying to a thread from 2009, please start your own thread with your specific question/needs. Thanks.
Copy linkTweet thisAlerts:
@NogDogFeb 16.2021 — {"locked":true}
×

Success!

Help @twoseasons 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 4.23,
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,
)...