Forum Moderators: open
I'm looking for an html code or javascript code that will show the current day and date, minus 2 days.
Example:
The current day and date is: Fri, April 27
I need the code that will display the date: Wed, April 25.
I would like the code to be cross platform and browser compatible, if possible.
Any help would be greatly appreciated!
Thank you!
and a warm welcome to these forums. ;)
Try it like this....
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>two days ago</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">
#ago2 {
width:460px;
padding:10px 0;
border:3px double #999;
font-family:verdana,arial,helvetica,sans-serif;
font-size:16px;
text-align:center;
margin:30px auto;
}
</style><script type="text/javascript">
window.onload=function(){
twoDaysAgo();
}
function twoDaysAgo() {days=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
now=new Date();
yy=now.getUTCFullYear();
mm=now.getUTCMonth();
dt=now.getUTCDate()-2;
dd=now.getUTCDay()-2;
backtwo=new Date(yy,mm,dt).toLocaleDateString();document.getElementById('ago2').firstChild.nodeValue='Two days ago it was '+days[dd]+' '+backtwo;
}
</script></head>
<body><div id="ago2"> </div>
</body>
</html>
birdbrain
<!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:utf-8">
<title>Date Example</title>
<script type="text/javascript">
// Create a global wrapper/namespace to avoid conflicts with other globals
var FOTI = window.FOTI ¦¦ {};
FOTI.clock = function() {
return {
showClock : function(el) {
var pastDate = new Date((new Date()).getTime() - (1000 * 60 * 60 * 48));
document.getElementById(el).appendChild(document.createTextNode(pastDate.toLocaleString()));
}
};
}();
window.onload = function() {
// Call your method when the page loads
FOTI.clock.showClock('dateContainer');
};
</script>
</head>
<body>
<div id="container">
<div id="dateContainer"></div>
</div>
</body>
</html>
now=new Date();
yy=now.getUTCFullYear();
mm=now.getUTCMonth();
dt=now.getUTCDate()-2;
backtwo=new Date(yy,mm,dt).toLocaleDateString();
dd=backtwo.getUTCDay();
thanks for pointing out the error. ;)
I must have suffered a brain fart.:)
Here is an amendment...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>two days ago</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">
#ago2 {
width:460px;
padding:10px 0;
border:3px double #999;
font-family:verdana,arial,helvetica,sans-serif;
font-size:16px;
text-align:center;
margin:30px auto;
}
</style><script type="text/javascript">
window.onload=function(){
twoDaysAgo();
}
function twoDaysAgo() {days=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
now=new Date();
yy=now.getUTCFullYear();
mm=now.getUTCMonth();
dt=now.getUTCDate()-2;
dd=now.getUTCDay();switch(dd) {
case 0:dd=5;
break;
case 1:dd=6;
break;case 2:dd=0;
break;case 3:dd=1;
break;case 4:dd=2;
break;case 5:dd=3;
break;case 6:dd=4;
break;
}backtwo=new Date(yy,mm,dt).toLocaleDateString();
document.getElementById('ago2').firstChild.nodeValue='Two days ago it was '+days[dd]+' '+backtwo;
}
</script></head>
<body><div id="ago2"> </div>
</body>
</html>
var pastDate = new Date((new Date()).getTime() - (1000 * 60 * 60 * 48));
What that's doing is creating a new date (which will use the current date) and calling getTime() on that date to return the value in milliseconds. Then all you need to do is subtract 2 days:
1000 milliseconds * 60 seconds = 1 minute
1 minute * 60 minutes = 1 hour
1 hour * 48 hours = 2 days
And ideally you should use toLocaleString to format the date. This way, it will be formatted correctly whether the person is viewing your site in the UK or the U.S., etc.
The "dd" bit depends if you like the format of toLocaleString, you may want to display the day/date/month/year separetely, in which case you'd use getDay from the new date object, like my first post.
But it doesn't account for local times, for example here in sunny England we're actually on BST, not GMT. BST is British Summer Time, and exists thanks to farmers who complain they haven't got anough daylight to milk their cows in the morning (or did a couple of hundred years ago), so the clocks go one hour forward.
Incidentally one of the recent optional Windows Updates was to correct the Australian clock, as they were trialling a similar daylight saving scheme for the next 3 years.
Now, a date system that doesn't use local time will report that it's 1 o'clock, when actually it's 12. Point well made Fotiman.
The "dd" bit depends if you like the format of toLocaleString, you may want to display the day/date/month/year separetely,
in which case you'd use getDay from the new date object, like my first post.
But it doesn't account for local times
Now, a date system that doesn't use local time will report that it's 1 o'clock, when actually it's 12.
That's because you were using the UTC methods instead of the local time methods. Here's how you should have done it if you wanted to format the string yourself.
1. Get the time for 2 days ago before you start splitting it up:
var pastDate = new Date((new Date()).getTime() - (1000 * 60 * 60 * 48));
yy = pastDate.getFullYear();
mm = pastDate.getMonth();
dt = pastDate.getDate();
var pastDate = new Date((new Date()).getTime() - (1000 * 60 * 60 * 48));yy = pastDate.getFullYear();
mm = pastDate.getMonth();
dt = pastDate.getDate();
That's what I was getting at in my last post, but thanks for claifying and posting the whole code.
I also didn't realise those methods were local time and not UTC. If I had done this I would have used toLocaleString and used split to chunk it up! :D
<!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:utf-8">
<title>Date Example</title>
<script type="text/javascript">
// Create a global wrapper/namespace to avoid conflicts with other globals
var FOTI = window.FOTI ¦¦ {};
FOTI.clock = function() {
return {
showClock : function(el) {
var pastDate = new Date((new Date()).getTime() - (1000 * 60 * 60 * 48));
document.getElementById(el).appendChild(document.createTextNode(pastDate.toLocaleString()));
}
};
}();
window.onload = function() {
// Call your method when the page loads
FOTI.clock.showClock('dateContainer');
};
</script>
</head>
<body>
<div id="container">
<div id="dateContainer"><!-- Place ServerSide Generated Date Here --></div>
</div>
</body>
</html>
Then the JavaScript date code becomes a true progressive enhancement.
[edited by: Fotiman at 9:18 pm (utc) on April 30, 2007]