Chapter 55. Adding a Dateline and Timestamp
A dateline is a piece of text that gives the current date, and it usually appears at the top of a page. Similarly, a timestamp is a piece of text that gives the current time, and it often appears at the bottom of a page. Both are handy for creating a sense of freshness. Nothing pleases your audience more than visiting an utterly current, up-to-the-second-fresh site. Even if you update your content on a sporadic basis, the appearance of the current date or the current time helps to make your site feel fresher.
GEEKSPEAK
A dateline is a piece of text that gives the current date. A timestamp is a piece of text that gives the current time.
Datelines and timestamps can be yours with some straightforward JavaScript functions. See the Toolkits for complete information.
[View full width]
function doDateline() {
/* First, get the current date. JavaScript's Date object does
var now = new Date();
/* The following lines extract the day of the week, the month,
var day = now.getDay();
var month = now.getMonth();
var date = now.getDate();
var year = now.getYear();
/* Internet Explorer returns the correct year, while Netscape and
if (year < 2000) {
year += 1900;
}
/* Initialize the dateline variable. This variable will
var dateline = ";
/* Now build the text of the dateline. The day of the week
Notice the comma and the trailing space after the name of the day
If you want a simple dateline, such as 12/22/2005, omit this
if (day == 0) {
dateline = "Sunday, ";
}
if (day == 1) {
dateline = "Monday, ";
}
if (day == 2) {
dateline = "Tuesday, ";
}
if (day == 3) {
dateline = "Wednesday, ";
}
if (day == 4) {
dateline = "Thursday, ";
}
if (day == 5) {
dateline = "Friday, ";
}
if (day == 6) {
dateline = "Saturday, ";
}
/* The month also exists in numerical form, with 0 for January, 1
If you want to use European style for your dateline, switch the
If you want a simple dateline, replace this block of code with
dateline += (month + 1) + "/";
*/
if (month == 0) {
dateline += "January ";
}
if (month == 1) {
dateline += "February ";
}
if (month == 2) {
dateline += "March ";
}
if (month == 3) {
dateline += "April ";
}
if (month == 4) {
dateline += "May ";
}
if (month == 5) {
dateline += "June ";
}
if (month == 6) {
dateline += "July ";
}
if (month == 7) {
dateline += "August ";
}
if (month == 8) {
dateline += "September ";
}
if (month == 9) {
dateline += "October ";
}
if (month == 10) {
dateline += "November ";
}
if (month == 11) {
dateline += "December ";
}
/* The next line appends the numerical date to the dateline and
For European formatting, switch this block with the preceding one
If you want a simple dateline, replace this line with the following:
dateline += date + "/";
*/
dateline += date + ", ";
/* The next line appends the numerical year to the dateline. */
dateline += year;
/* Your dateline is ready for display. This line writes the
document.write(dateline);
}
So much for the main script. Now, in the place where you want the dateline to appear on your page, add the following code:
<script language="JavaScript">doDateline();</script>
If you prefer, you may mark up this script tag as a paragraph, header, or any other type of text element:
<p><script language="JavaScript">doDateline();</script></p>
Put the main script together with the doDateline callout, and you get something that looks like Figure 55.1. The simple version looks like Figure 55.2.
Figure 55.1. Add a dateline to any Web page.
[View full size image]

Figure 55.2. If you prefer, you can create a simple dateline.
[View full size image]

TIP
The browser renders the dateline in the visitor's local time or whatever time the internal clock of the visitor's computer shows.
[View full width]
function doTimestamp() {
/* Like before, begin by getting the current date and putting it
var now = new Date();
/* Mine the now variable for the hour, minutes, and seconds. */
var hour = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
/* Initialize the timestamp variable, which will hold the string
var timestamp = ";
/* Set the time variable to A.M., with a preceding space for
var time = " A.M.";
/* JavaScript hours use the 24-hour format by default. The
If you want to display 24-hour format, replace this block of code
if (hour < 10) {
hour = "0" + hour;
}
This if/then block adds a zero to the left of the hour where
if (hour > 12) {
hour -= 12;
time = " P.M.";
}
/* Similarly, these next if/then blocks format minutes and
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
/* Now build up the timestamp string. */
timestamp = hour + ":" + minutes + ":" + seconds + time;
/* Write the timestamp into the page. */
document.write(timestamp);
}
Add the following code in the place where you want the timestamp to appear on your page:
<script language="JavaScript">doTimestamp();</script>
You probably want to juice it up with some extra formatting:
<p>
<em>This page generated at
<script language="JavaScript">doTimestamp();</script></em>
</p>
<hr noshade
See the results for yourself in Figure 55.3.
Figure 55.3. Add a timestamp.
[View full size image]

TIP
As with the dateline, this timestamp script generates times according to the internal clock of the visitor's computer.