Forum Moderators: open

Message Too Old, No Replies

How To Display Earlier Date

         

p33h

7:35 pm on Mar 1, 2009 (gmt 0)

10+ Year Member



Hi,

I'm using javascript to display the date which was 4 days before. I used "date=date-4;" to set back the date, but whenever it is 1st, 2nd, 3rd or 4th of the month it displays negative number in the day such as -3 March 2009. Is it possible to modify it somehow to display the date that was 4 days before which will work with first days of the month?

Here is my code that I use:

<script language="JavaScript1.2">
<!-- Begin
var months=new Array(13);
months[1]="Stycznia";
months[2]="Lutego";
months[3]="Marca";
months[4]="Kwietnia";
months[5]="Maja";
months[6]="Czerwca";
months[7]="Lipca";
months[8]="Sierpnia";
months[9]="Wrze&#347;nia";
months[10]="Pa&#378;dziernika";
months[11]="Listopada";
months[12]="Grudnia";
var time=new Date();
var lmonth=months[time.getMonth() + 1];
var date=time.getDate();
var year=time.getYear();
date=date-4;
if (year < 2000) // Y2K Fix, Isaac Powell
year = year + 1900;
document.write("<b>" + date + " ");
document.write(lmonth + ", " + year + "</b>");
// End -->
</script>

Thanks!

[edited by: eelixduppy at 7:45 pm (utc) on Mar. 1, 2009]
[edit reason] specifics [/edit]

birdbrain

8:49 pm on Mar 1, 2009 (gmt 0)



Hi there p33h,

try it like this...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title></title>

<style type="text/css">
#mydate {
font-weight:bold;
}
</style>

<script type="text/javascript">
if(window.addEventListener){
window.addEventListener('load',showDate,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',showDate);
}
}

function showDate(){

var months=[
'Stycznia',
'Lutego',
'Marca',
'Kwietnia',
'Maja',
'Czerwca',
'Lipca',
'Sierpnia',
'Wrze&#347;nia',
'Pa&#378;dziernika',
'Listopada',
'Grudnia'
];

now=new Date(); /* This is today */

yy=now.getUTCFullYear();
mm=now.getMonth();
dd=now.getDate()-4; /* This is 4 days ago */

then=new Date(yy,mm,dd);
yy=then.getUTCFullYear();
mm=months[then.getMonth()];
dd=then.getDate();

document.getElementById('mydate').firstChild.nodeValue=dd+' '+mm+', '+yy;

}

</script>

</head>
<body>

<div id="mydate">&nbsp;</div>

</body>
</html>


birdbrain

p33h

10:28 pm on Mar 1, 2009 (gmt 0)

10+ Year Member



It works! Thanks man :*

birdbrain

10:41 pm on Mar 1, 2009 (gmt 0)



No problem, you're very welcome. ;)

alxstatik

2:34 pm on Apr 3, 2009 (gmt 0)

10+ Year Member



I was having the same problem and I tried the code but I get no display. What am I doing wrong? I copied and pasted that exact code onto my site where I want it and then I tried it on a blank page and still do not get the date to display.