以下是一个 jQuery 示例,可以显示当前时间和星期几,以及是上午还是下午。

HTML 代码:

<div id='clock'></div>

jQuery 代码:

$(document).ready(function() {
  setInterval(function() {
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();
    var ampm = hours >= 12 ? '下午' : '上午';
    hours = hours % 12;
    hours = hours ? hours : 12;
    minutes = minutes < 10 ? '0' + minutes : minutes;
    seconds = seconds < 10 ? '0' + seconds : seconds;
    var time = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
    var weekday = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
    var day = weekday[now.getDay()];
    $('#clock').html(time + ' ' + day);
  }, 1000);
});

这段代码会在页面中创建一个<div>元素,其 ID 为 clock。然后,使用 setInterval() 函数每秒钟更新一次时钟。日期和时间信息从 Date 对象中获取,并使用一些逻辑来确定当前时间是上午还是下午。最后,使用 jQuery 的 html() 函数将时间和日期信息显示在页面上。

jQuery 获取当前时间、星期几和上午/下午

原文地址: https://www.cveoy.top/t/topic/lMfw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录