首頁 > web前端 > js教程 > JavaScript在瀏覽器標題列上顯示目前日期和時間的方法_javascript技巧

JavaScript在瀏覽器標題列上顯示目前日期和時間的方法_javascript技巧

WBOY
發布: 2016-05-16 16:08:21
原創
1462 人瀏覽過

本文實例講述了JavaScript在瀏覽器標題列上顯示當前日期和時間的方法,分享給大家供大家參考。具體如下:

將這段腳本放到head區即可:

<script language="JavaScript">
<!--
function resetIt() {
// Calculate Time
var timerID = null;
var timerRunning = false;
if(timerRunning)
  clearTimeout(timerID);
  timerRunning = false;
// getTime
var timeNow = new Date();
var hours = timeNow.getHours();
var minutes = timeNow.getMinutes();
var seconds = timeNow.getSeconds();
var timeValue = "" + ((hours >12) &#63; hours -12 :hours)
timeValue = ((timeValue <10)&#63; "0":"") + timeValue
timeValue += ((minutes < 10) &#63; ":0" : ":") + minutes
timeValue += ((seconds < 10) &#63; ":0" : ":") + seconds
timeValue += (hours >= 12) &#63; " PM" : " AM"
timerID = setTimeout("resetIt()",100);
timerRunning = true;
// getDate
var dateNow = new Date();
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((dateNow.getDate()<10) &#63; "0" : "")+ dateNow.getDate();
function y2k(number){return (number < 1000) &#63; number + 1900 : number;}
// compileIt
today = timeValue + " " + days[dateNow.getDay()] + " " +
        months[dateNow.getMonth()] + ", " +
        date + " " +
        (y2k(dateNow.getYear()));
  if(document.all || document.getElementById){ // Browser Check
   document.title = today.toString();
  }else{
   self.status = today.toString(); // Default to status.
  }
}
resetIt();
//-->
</script>
登入後複製

希望本文所述對大家的javascript程式設計有所幫助。

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板