Forum Moderators: open

Message Too Old, No Replies

Show date on a webpage

Code needed to show day and date on a website

         

chadhenry11

3:24 pm on Apr 27, 2007 (gmt 0)

10+ Year Member



Hi everyone,

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!

birdbrain

3:52 pm on Apr 27, 2007 (gmt 0)



Hi there chadhenry11,

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">&nbsp;</div>

</body>
</html>

birdbrain

Fotiman

4:02 pm on Apr 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here's one way you might do it:


<!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>

Dabrowski

9:48 pm on Apr 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Both work, but birdbrain if the day of month is less than 2, i.e. the 1st or 2nd of the month, the day comes out as undefined. You need to make this small change:

now=new Date();
yy=now.getUTCFullYear();
mm=now.getUTCMonth();
dt=now.getUTCDate()-2;
backtwo=new Date(yy,mm,dt).toLocaleDateString();
dd=backtwo.getUTCDay();

birdbrain

10:25 am on Apr 28, 2007 (gmt 0)



Hi there Dabrowski,

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">&nbsp;</div>

</body>
</html>


birdbrain

Dabrowski

11:31 am on Apr 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Man your brain must have had a strong curry!

Just read dd off the new data object, backtwo. Save all that switch/case nonsense!

Alternatively, use the mod operator:

dd = (( now.getUTCDay() -2 +7) % 7);

See, you add an entire week on, then 'wrap' it to 7 days.

Fotiman

2:09 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Guys, that's way too out there. All you need to do to get the date for 2 days ago is this:

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.

Dabrowski

2:45 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes that is the simplest way to create the new date object.

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.

Fotiman

3:34 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




The "dd" bit depends if you like the format of toLocaleString, you may want to display the day/date/month/year separetely,

True. But keep in mind that if you format it yourself, it may not make sense for other locales.


in which case you'd use getDay from the new date object, like my first post.

Not exactly. In your first post, you were getting the current date and splitting it up first, and THEN trying to subtract 2 days. Instead, you should still be subtracting the 2 days first.


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));

2. Parse the date into its individual parts using the local time methods:

yy = pastDate.getFullYear();
mm = pastDate.getMonth();
dt = pastDate.getDate();

Dabrowski

3:42 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

templeofboom

6:53 pm on Apr 30, 2007 (gmt 0)

10+ Year Member



There are more than a few people in the world with javascript disabled. What about a simple Server Side Include? Should be a one or two liner, depending on the syntax.

After all, just inserting the current date is:

<!--#echo var="DATE_LOCAL" -->

Fotiman

9:18 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As always, a server side solution is the best option. In fact, my original example could include the server-side generated date within the dateContainer when the page loads:


<!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]