Click to See Complete Forum and Search --> : Date script


dcjones
01-30-2003, 03:26 PM
Hi all,

I have a date script which run OK, but , what I want it to do is produce the date minus one month.

i.e if todays date is January 30th 2003 I want the script to display
December 30th 2003.

The Script:

<script language="JavaScript">
<!--
dayName = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
monName = new Array ("January", "February", "March"," April", "May", "June", "July", "August", "September", "October", "November", "December")
now = new Date
--!>
</script>

</HEAD>

<BODY>
<script>
document.write("<p align=center><font=arial color=#CC0000> Page last updated on " +
dayName[now.getDay()] + ", "+
monName[now.getMonth()] + " " +
now.getDate() + ".</font><\/p>")
</script>
</body>
</html>

Anyone have any ideas.

Regards and thanks in advance

Dereck

dcjones
01-30-2003, 03:44 PM
Hi all

Please forget this post I have sorted it out myself

I just changed the month array order and it works fine


Thanks


Dereck

Charles
01-30-2003, 04:01 PM
If you are concerned about the year then try this:

<script type="text/javascript">
<!--

Date.SECOND = 1000;
Date.MINUTE = Date.SECOND * 60;
Date.HOUR = Date.MINUTE * 60;
Date.DAY = Date.HOUR * 24;

Date.prototype.getOneMonthBefore = function () {
oneMonth = [31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30];
if (this.getFullYear() % 4 == 0 && this.getFullYear() % 20 != 0) ++oneMonth[2];
return new Date(this.getTime() - oneMonth[this.getMonth()] * Date.DAY);
};

Date.prototype.toString = function () {return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'] [this.getDay()], this.getDate(), ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] [this.getMonth()], this.getFullYear()].join('&nbsp;')}

document.write('<p>One month ago it was:', new Date().getOneMonthBefore(), '</p>')
// -->
</script>

dcjones
01-30-2003, 04:31 PM
Hi Charles,

Once again you come to the rescue. your script work perfectly.

Thank you again

Dereck

Charles
01-31-2003, 05:11 AM
I've been giving some thought to makeing a more general purpose version of that script - one method that would return a date object any number of months before or after. But I've run into a bit of trouble. What's the definition of "one month away"? A month from 1 January is clearly 1 February, but what about a month from 31 January?

IxxI
01-31-2003, 05:27 AM
Well it depends. If you want to keep the same months then it would have to be 28th (or 29th) of February. However if you wnated to have 12 equal segments to represent months then January 31st would be in the second (there being approx. 30.5 days per month). It would seem far less complicated to keep to the traditional method of month naming or you'll run into problems with fractions (its actually 30.41666666 etc. days per "month" if each month is equal).
IxxI