Forum Moderators: open
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śnia";
months[10]="Paź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]
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śnia',
'Paź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"> </div>
</body>
</html>
No problem, you're very welcome. ;)