Forum Moderators: open

Message Too Old, No Replies

JS to display future date?

Sunday, Monday, Tuesday format...

         

Yume

12:55 am on Oct 10, 2009 (gmt 0)

10+ Year Member



I'm trying to display a date with JS, 2 days in the future.

But I'm having problems figuring out how to do it for the day of the week.

I want it to say:
"Product will be shipped on Monday, October 13"

Does anyone know of a script for this?

whoisgregg

10:47 pm on Oct 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, Yume!

Could you post what code you've tried so far? :)

Yume

11:00 pm on Oct 11, 2009 (gmt 0)

10+ Year Member



Thanks! :)
I've been a curious lurker for a while.

The code is below. It works on most days of the week, except the skip from Saturday to Sunday.

I have to somehow tell it to loop the days of the week.

So on Fridays, the 2 day skip won't work, and it just writes "undefined". All the other days of the week work fine though.


<script language="JavaScript">
<!--
var now = new Date();
var days = new Array(
'Sunday','Monday','Tuesday',
'Wednesday','Thursday','Friday','Saturday');
var months = new Array(
'January','February','March','April','May',
'June','July','August','September','October',
'November','December');
var date = now.getDate()+2;

today = days[now.getDay()+2] + ", " +
months[now.getMonth()] + " " +
date;

//-->
</script>

<script type="text/javascript">document.write(today);</script>

whoisgregg

11:20 pm on Oct 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're almost there! :)

To add/subtract dayes, you have to do it on the Date object itself, not on the functions that return the data. In your existing code, you'll also have a problem when you are one day from the end of the month (because you'll end up displaying the current month no matter what right now.

But, like I said, you're practically there in the code with your definition of the date variable, the trick is to define that properly and then use it when you output the day and month:

<script language="JavaScript"> 
<!--
var days = new Array(
'Sunday','Monday','Tuesday',
'Wednesday','Thursday','Friday','Saturday');
var months = new Array(
'January','February','March','April','May',
'June','July','August','September','October',
'November','December');
var now = new Date();
now.setDate(now.getDate()+2);
// now.setDate(33); // for debugging, to see how it handles rollover dates
today = days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
now.getDate();
//-->
</script>
<script type="text/javascript">document.write(today);</script>

Yume

11:39 pm on Oct 11, 2009 (gmt 0)

10+ Year Member



Muchas gracias! :)

whoisgregg

1:19 am on Oct 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Happy to help and happy to see a lurker posting. :)