|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today! |
|
#1
|
|||
|
|||
|
Out putting time and date using JavaScript.
I am using the following code for printing the date and time in the web site. But I would like to keep this code in a exterenal file and call it in side the html code for showing date and time. How to do that?
<SCRIPT LANGUAGE="JavaScript"> <!-- Stamp = new Date(); document.write('<B>Todays date:</b> ' '' + (Stamp.getMonth() + 1) +"/"+Stamp.getDate()+ "/"+Stamp.getYear() + ' <br> '); var Hours; var Mins; var Time; Hours = Stamp.getHours(); if (Hours >= 12) { Time = " P.M."; } else { Time = " A.M."; } if (Hours > 12) { Hours -= 12; } if (Hours == 0) { Hours = 12; } Mins = Stamp.getMinutes(); if (Mins < 10) { Mins = "0" + Mins; } document.write('<B>Todays Time:</b> ' + Hours + ":" + Mins + Time + ''); //--> </SCRIPT> |
|
#2
|
|||
|
|||
|
RE: Out putting time and date using JavaScript.
save the javascript (without the script tags) to a file and name it time.js. Then add the following code to your html page where you want the date and time to show up.
<script src="time.js" type="text/javascript"></script> |
|
#3
|
|||
|
|||
|
RE: Out putting time and date using JavaScript.
Thank you for your suggestion. I would like to keep the code inside the function. So that I would like to call the function. How to do it.? |
|
#4
|
|||
|
|||
|
RE: Out putting time and date using JavaScript.
first wrap the javascript code inside a function in the .js file.
function GetDateTime(){ //your code here } then place the link to the file between the head tags of the html page. <head> <script src="time.js" type="text/javascript"></script> </head> then wherever you want to display the date and time, put this in... <script>GetDateTime()</script> |
|
#5
|
|||
|
|||
|
RE: Out putting time and date using JavaScript.
Now working with a new code for showing date and time. It works perfectly when JS code in under the html tag. But I would like to keep the JS code in an external file and call the file inside the html. I tired to do so but it's giving me error and the blinker is not blinking when I tried. Any suggestion. Thank you.
Code:
<HTML>
<HEAD>
<title>DCScript© Digital Clock</title>
<style>
.clock {
background-color: #A6A98D;
color: black;
font-family: verdana;
float: center;
border: 1px solid black;
border-collapse: collapse;
}
.time {
font-size: 100;
}
.clock TD {
border: 1px solid black;
border-collapse: collapse;
}
B {color:black}
BODY {font-family: arial; font-size: 10pt}
</style>
<SCRIPT LANGUAGE="JavaScript">
function time() {
var today = new Date();
var hrs = today.getHours();
var min = today.getMinutes();
var secs = today.getSeconds();
var alsohrs = today.getHours();
var dayNumber = today.getDate();
var year = today.getFullYear();
var ampm="";
var zero="0";
var month = today.getMonth() +1;
var weekday = today.getDay();
var wdn = new Array(7)
wdn[0] = "SUN";
wdn[1] = "MON";
wdn[2] = "TUE";
wdn[3] = "WED";
wdn[4] = "THU";
wdn[5] = "FRI";
wdn[6] = "SAT";
//Statement that puts '0's in front of single minutes or seconds.
if (min<10)
{
min=zero+min;
}
if (secs<10)
{
secs=zero+secs;
}
//Statement that eliminates Metric Time
if (hrs>12)
{
hrs=eval(hrs - 12);
}
if (hrs>=0 && hrs<1)
{
hrs=12
}
//P.M. Statement
if (alsohrs>=12 && alsohrs<24)
{
ampm="P.M.";
}
//A.M. Statement
if (alsohrs<12 && alsohrs>=0)
{
ampm="A.M.";
}
tmp='<table width="60%" class="clock"><tr><td class="time" colspan="4">';
tmp+=hrs+'<span id="blinker">:</span>'+min;
tmp+='<font size="20"> '+ampm+'</font>';
tmp+='<tr><td><font size="-1">Month</font><br><b>'+month+'</b></td>';
tmp+='<td><font size="-1">Date</font><br><b>'+dayNumber+'</b></td>';
tmp+='<td><font size="-1">Day</font><br><b>'+wdn[weekday]+'</b></td>';
tmp+='<td><font size="-1">Year</font><br><b>'+year+'</b></td></tr></table>';
document.getElementById("clockgoeshere").innerHTML=tmp;
clocktime=setTimeout("time()","1000");
}
function blink() {
var obj = document.getElementById("blinker");
if (obj.style.visibility == "visible") {
obj.style.visibility="hidden";
}
else {
obj.style.visibility="visible";
}
eachsecond=setTimeout("blink()","500");
}
</SCRIPT>
</head>
<body onLoad="time(); blink();"> <center>
<div id="clockgoeshere"></div><p>
</center>
</body>
</body>
</html>
|
|
#6
|
|||
|
|||
|
RE: Out putting time and date using JavaScript.
your .js file would look like this...
---time.js----- function time() { var today = new Date(); var hrs = today.getHours(); var min = today.getMinutes(); var secs = today.getSeconds(); var alsohrs = today.getHours(); var dayNumber = today.getDate(); var year = today.getFullYear(); var ampm=""; var zero="0"; var month = today.getMonth() +1; var weekday = today.getDay(); var wdn = new Array(7) wdn[0] = "SUN"; wdn[1] = "MON"; wdn[2] = "TUE"; wdn[3] = "WED"; wdn[4] = "THU"; wdn[5] = "FRI"; wdn[6] = "SAT"; //Statement that puts '0's in front of single minutes or seconds. if (min<10) { min=zero+min; } if (secs<10) { secs=zero+secs; } //Statement that eliminates Metric Time if (hrs>12) { hrs=eval(hrs - 12); } if (hrs>=0 && hrs<1) { hrs=12 } //P.M. Statement if (alsohrs>=12 && alsohrs<24) { ampm="P.M."; } //A.M. Statement if (alsohrs<12 && alsohrs>=0) { ampm="A.M."; } tmp='<table width="60%" class="clock"><tr><td class="time" colspan="4">'; tmp+=hrs+'<span id="blinker">:</span>'+min; tmp+='<font size="20"> '+ampm+'</font>'; tmp+='<tr><td><font size="-1">Month</font><br><b>'+month+'</b></td>'; tmp+='<td><font size="-1">Date</font><br><b>'+dayNumber+'</b></td>'; tmp+='<td><font size="-1">Day</font><br><b>'+wdn[weekday]+'</b></td>'; tmp+='<td><font size="-1">Year</font><br><b>'+year+'</b></td></tr></table>'; document.getElementById("clockgoeshere").innerHTML=tmp; clocktime=setTimeout("time()","1000"); } function blink() { var obj = document.getElementById("blinker"); if (obj.style.visibility == "visible") { obj.style.visibility="hidden"; } else { obj.style.visibility="visible"; } eachsecond=setTimeout("blink()","500"); } them your html file will look like this... <HTML> <HEAD> <title>DCScript© Digital Clock</title> <style> .clock { background-color: #A6A98D; color: black; font-family: verdana; float: center; border: 1px solid black; border-collapse: collapse; } .time { font-size: 100; } .clock TD { border: 1px solid black; border-collapse: collapse; } B {color:black} BODY {font-family: arial; font-size: 10pt} </style> <script src="time.js" type="text/javascript"></script> </head> <body onLoad="time(); blink();"> <center> <div id="clockgoeshere"></div><p> </center> </body> </body> </html> Unless I am completly misunderstanding what you want to do, this should work as expected. I tested it and it works fine. |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Client Side Things > Out putting time and date using JavaScript. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|