View Full Version : Dual Clock
Sunset
09-28-2008, 05:01 PM
Is there a way to put a dual display on a web page of both local standard time and also in military time?
(i.e. "The current time is 1:45 pm / 1345.")
Here:
<script type="text/javascript">
var dispTime = function(){
var timeUpd = new Date();
var localTime = timeUpd.getHours();
if(localTime > 12){
localTime -= 12;
}
var ext;
if(localTime > 11){
ext = "PM";
} else {
ext="AM";
}
localTime = localTime+":"+timeUpd.getMinutes()+" "+ext;
var armTime = timeUpd.getHours()+":"+timeUpd.getMinutes()+" "+ext;
return localTime+"/"+armTime;
}
</script>
The dispTime() function should return the dates. To execute:
<script type="text/javascript">
alert(dispTime());
</script>
Mohawk Man
09-28-2008, 09:28 PM
hey on mine it says its AM and not PM. is that on anyone elses?
Sunset
09-29-2008, 01:52 AM
Thanks, but mine (http://dennier.com/test/) is also showing AM (when it should currently be PM). It's also showing AM on the military half of the display (when military time does not display AM or PM).
Also, is there a way to have the time display on the website itself, rather than as a popup?
jscheuer1
09-29-2008, 06:13 AM
I have a question, you mean the user's local time or the local time of the website? Both Nile's version and the one below are the user's local time:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
#timeDisplay {
font: normal 90% sans-serif;
}
#timeDisplay b {
color: #f00;
}
</style>
<script type="text/javascript">
function time12_24(){
var el = document.getElementById('timeDisplay'),
d = new Date(),
h = d.getHours(),
m = d.getMinutes(),
b = document.createElement('b');
m = m < 10? '0' + m : m;
while (el.lastChild) el.removeChild(el.lastChild);
b.appendChild(document.createTextNode((h%12 || 12) + ':' + m + (h < 12 ? ' am' : ' pm') + ' / ' + (h < 10? '0' + h : h) + m + ' hrs'));
el.appendChild(document.createTextNode('The current time is - '));
el.appendChild(b);
};
window.onload = function(){
time12_24();
setInterval(time12_24, 1000);
};
</script>
</head>
<body>
<div id="timeDisplay"> </div>
</body>
</html>
Sunset
09-29-2008, 10:28 AM
Yes! Thank you, jscheuer1 - This is exactly what I was looking for. The visitor's local time in both 12-hour and 24 hour formats together.
I appreciate the responses and the help!
John, why do you do functions like this?:
function time12_24(){
Mburt told me that the new way to do them is like this:
var time12_24 = function(){
I'm just curious..
jscheuer1
09-29-2008, 03:29 PM
John, why do you do functions like this?:
function time12_24(){
Mburt told me that the new way to do them is like this:
var time12_24 = function(){
I'm just curious..
I'm not aware of any advantage in writing a function definition in that manner, unless it is part of another function and is going to be used in certain ways within the scope of that function.
Can you point out any case where there is an advantage in using the:
var name = function(){}
syntax for functions defined in the global scope? Perhaps you could link to a post in which Mburt explains this, or to some other resource that does.
When code is well written, using:
function name(){}
can distinguish a function as being in the global scope.
However, I have never been one to keep up on all the very latest practices - many of which are just a matter of opinion, and/or extremely browser specific as far as their value goes (meaning it may work cross browser, yet have no real advantage except in a specific browser). It is enough for me that code be valid and work. I also do like code to be, if not as efficient as possible, at least not so poorly constructed that it causes problems due to execution speed. I'm always willing to learn, though.
I asked mburt why he said its newer, idk you'll have to ask him.
jscheuer1
09-30-2008, 02:31 AM
I asked mburt why he said its newer, idk you'll have to ask him.
As far as I know, it's not new, just different and has implications as I stated when used within a limited scope, but not in the global scope (like the function of mine that you were asking about). I'll ask him if it ever comes up in a thread we both participate in.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.