那里有很多 javascript 时钟,您甚至不必使用 API 来获取时间和日期!
function clock(id) {
    //Create a new Date object.
    oDate = new Date();
    //Get year (4 digits)
    var year = oDate.getFullYear();
    //Get month (0 - 11) - NOTE this is using indexes, so 0 = January.
    var month = oDate.getMonth();
    //Get day (1 - 31) - not using indexes.
    var day = oDate.getDate();
   //Get hours
   var hours = oDate.getHours();
   //Get minutes
   var minutes = oDate.getMinutes();
   //Get seconds
   var seconds = oDate.getSeconds();
   //Maybe create a function that adds leading zero's here
   var dateStr = '';
   dateStr += year+' - '+month+' - '+day+' '+hours+':'+minutes+':'+seconds;
   //Append dateStr to some element.
   document.getElementById(id).innerHTML = dateStr;
   //Repeat the function every 1000 miliseconds aka 1 second
   setTimeout(function() {
        clock(id);
   }, 1000);
}
用法是
<div id="yourID">clock input will go here</div>
clock('yourID');
笔记
该函数必须在 DOM 加载完成后调用,否则会报错。
这可以通过将带有 JS 的脚本标签放在页面底部来实现(不使用 jQuery)。
否则,如果使用 jQuery,则调用$(function() {})(等价于$(document).ready(function() {});
该功能是不言自明的,但也许您想阅读这些功能以确切了解它们的作用。
快速的谷歌搜索应该可以解决问题。
无论如何希望这会有所帮助,祝你好运:)