Dates, dates, and more dates...
We get a lot of requests from people who want to put the current date on their Web pages, so I thought I would start my first official FrontPage tip with code to help accomplish this.
There are a couple ways that you can get the current date onto your Web pages. One uses client-side JavaScript, the other uses server-side code. As always, there are advantages and disadvantages to both. The code I'm including in this post is for client-side JavaScript because generally you want the user to see the date that they have on their local machine. Server-side dates take their dates from the server, which could be all the way on the other side of the world. Probably the only disadvantage to JavaScript is that users can turn scripting off in their browser. However, if they do this, the worst that happens is that the visitor doesn't see the date.
Just because I like to be difficult, I'm including several renditions of the code, each display the date slightly differently.
To use any of these you just insert the one you want into a script block in the HEAD section of your page, and put an inline script block, like the following, that names the function, where "GetTodaysDate" in the following example is the name of the function.
<script language="javascript">document.write(GetTodaysDate());</script>
Dates and Times
6/11/2004:
This one displays the current date in short format:
function GetTodaysDate(){
var oDate = new Date();
var sDate = oDate.getMonth() + "/" + oDate.getDate() + "/" + oDate.getYear()
return sDate;
}
Friday, 6/11/2004:
This one displays the day of the week and then the current date in short format:
function GetTodaysDayAndDate(){
var oDate = new Date();
var iDay = oDate.getDay();
var sDay;
switch (iDay){
case (0) :{sDay = "Sunday"; break;}
case (1) :{sDay = "Monday"; break;}
case (2) :{sDay = "Tuesday"; break;}
case (3) :{sDay = "Wednesday"; break;}
case (4) :{sDay = "Thursday"; break;}
case (5) :{sDay = "Friday"; break;}
case (6) :{sDay = "Saturday"; break;}
}
var sDate = oDate.getMonth() + "/" + oDate.getDate() + "/" + oDate.getYear()
return sDay + ", " + sDate;
}
Fri Jun 11 2004:
This one displays the day of the week and then the current date in pseudo short format:
function GetTodaysDateLongFormat(){
var oDate = new Date();
var sDate = oDate.toDateString();
return sDate;
}
17:32:
his one shows the time. It's in military format, but the following example uses code that uses AM and PM.
function GetTime(){
var oDate = new Date();
var sTime = oDate.getHours() + ":" + oDate.getMinutes();
return sTime;
}
5:32 PM:
This one shows the time in AM / PM format.
function GetTimeLong(){
var oDate = new Date();
var iHour = oDate.getHours();
var sAmPm;
if (iHour < 13)
{
sAmPm = "AM";
}
else
{
iHour = iHour - 12;
sAmPm = "PM";
}
var sTime = iHour + ":" + oDate.getMinutes() + " " + sAmPm;
return sTime;
}
6/11/2004, 5:32 PM:
And, finally, this one shows you date AND time.
function GetDateTime(){
var oDate = new Date();
var sDate = oDate.getMonth() + 1 + "/" + oDate.getDate() + "/" + oDate.getYear();
var iHour = oDate.getHours();
var sAmPm;
if (iHour < 13)
{
sAmPm = "AM";
}
else
{
iHour = iHour - 12;
sAmPm = "PM";
}
var sTime = iHour + ":" + oDate.getMinutes() + " " + sAmPm;
return sDate + ", " + sTime;
}
Comments
- Anonymous
June 11, 2004
If the user is on the other side of the world than your server than the fact that he speaks different language than English or speaks English but uses different date format is going to be bigger problem then the time offset... Not a very good argument against server side code :) - Anonymous
June 11, 2004
Am I the only one that prefers to use ISO 8601 dates in everyday life? I always write out 2004-06-12... So clean and unambiguous! - Anonymous
June 14, 2004
The comment has been removed - Anonymous
July 13, 2004
How do you do this in SharePoint using FrontPage as it does not allow JavaScript on WebPartPages? - Anonymous
July 13, 2004
The comment has been removed - Anonymous
April 23, 2005
Hi. I've copied the script :
function GetTodaysDateLongFormat(){
var oDate = new Date();
var sDate = oDate.toDateString();
return sDate;
}
but i can't see anything, even after i've uploaded it. What am I doing wrong? can you offer a solution?? - Anonymous
February 14, 2006
Hi, I have this in my table
Beginning End
22/04/2003 10:00 23/04/2003 01:00
What is the query that can tell me the number of minutes between them??For this Eaxample, I want the query to show me this:
Beginning End Interval
22/04/2003 23:00 23/04/2003 01:00 120 minutes - Anonymous
February 14, 2006
Hi, I have this in my table
Beginning End
22/04/2003 10:00 23/04/2003 01:00
How to do it in sharepoint - can tell me the time difference between them??For this Eaxample, I want to show:
Beginning End Interval
22/04/2003 23:00 23/04/2003 01:00 120 minutes
please reply to sadiq@giordano.com.sa - Anonymous
March 20, 2006
Thanks very much. I have had this questions for a long time now!! - Anonymous
May 01, 2006
The comment has been removed - Anonymous
July 12, 2006
The comment has been removed - Anonymous
July 13, 2006
The comment has been removed - Anonymous
July 29, 2006
I want to add several different time zones, "times" to a web page can you help - Anonymous
July 29, 2006
the three I wanted to add were NY, London and Tokyo - Anonymous
August 27, 2006
The comment has been removed