Forum Moderators: open

Message Too Old, No Replies

Generate a date for 3 weeks time

How do i do it?

         

Trenton

12:42 pm on Jan 28, 2004 (gmt 0)

10+ Year Member



Hi

I'd like to generate a date on my site with JavaScript that's 3 weeks from the time my site users are on the web.

So, if you're on my website on 2nd February then the displayed date will be 23rd Februuary.

Anyone know how to do this?

BlobFisk

1:24 pm on Jan 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Trenton,

This is not going to be straightforward! To get todays day (of the month), you would use the Date() method:


var today=new Date();
dayOfMonth = today.getDate();
monthOfYear = today.getMonth();

To add 21 days onto your day is not as simple as:

dayOfMonth = dayOfMonth+21;

Because, taking todays date (Wed the 28th) and adding 21 will give you 49!

You will need to work out the logic of this by taking what month of the year it is, and from there working out how to calculate the date when adding 3 weeks spans 2 months.

HTH

BitBanger

2:18 pm on Jan 28, 2004 (gmt 0)

10+ Year Member



deleted

TrinkDawg

3:52 pm on Jan 28, 2004 (gmt 0)

10+ Year Member



Maybe try something like this:

<html>
<head>
</head>
<body>
<script language="JavaScript">
<!--
var today = new Date();
var month = today.getMonth() + 1;
var day = today.getDate();
var year = today.getFullYear();
document.writeln('Today is: ' + month + '/' + day + '/' + year);
var daysInMonth= new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
/* adjust for leap year */
if (((year%4 == 0)&&(year%100!= 0)) ¦¦ (year%400 == 0))
{
daysInMonth[2] = 29;
}
var numDays = daysInMonth[month];
for (var i=0; i<21; i++)
{
day++;
if (day > numDays)
{
day = 1;
month++;
if (month == 13)
{
month = 1;
year++;
}
numDays = daysInMonth[month];
}
}
document.writeln('<br>');
document.writeln('Three weeks from now is: ' + month + '/' + day + '/' + year);
-->
</script>
</body>
</html>

HTH

DrDoc

5:21 pm on Jan 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's actually a lot easier than that :)

<script type="text/javascript">
now = new Date();
threeweeksfromnow = new Date(now.getTime() + (21 * 24 * 60 * 60 * 1000));
document.write(now + "<br>" + threeweeksfromnow)
</script>

[edited by: DrDoc at 5:24 pm (utc) on Jan. 28, 2004]

BlobFisk

5:23 pm on Jan 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Beautiful!

Nicely done Sir..

Trenton

6:24 pm on Jan 28, 2004 (gmt 0)

10+ Year Member



DrDoc, you're a JavaScript genius! Sorry to ask, but how can I write it without the time or year? Thanks for your help everyone!

DrDoc

12:18 am on Jan 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<script type="text/javascript">
now = new Date();
threeweeksfromnow = new Date(now.getTime() + (21 * 24 * 60 * 60 * 1000));
months = new Array("January ","February ","March ","April ","May ","June ","July ","August ","September ","October ","November ","December ");

document.write("Today's date is: " + months[now.getMonth()] + now.getDate() + "<br>");

document.write("Three weeks from today is: " + months[threeweeksfromnow.getMonth()] + threeweeksfromnow.getDate() + "<br>");
</script>

Trenton

8:39 am on Jan 29, 2004 (gmt 0)

10+ Year Member



Thanks DrDoc!