欢迎各位兄弟 发布技术文章
这里的技术是共享的
最后的效果是 下面的时分秒会不停的变化
html 源代码 <span class='get_current_date_time'> 2023-10-16 22:57:27</span>
jQuery(document).ready(function(){
curr_date_time = jQuery('.get_current_date_time').html();
curr_date_time = curr_date_time.split(" ");
specificDate = new Date(curr_date_time[0]+"T"+curr_date_time[1]);
// 每隔1秒钟更新时间
setInterval(function(){
var currentTime = specificDate;
var years = currentTime.getFullYear();
var months = currentTime.getMonth()+1;
var days = currentTime.getDate();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
// 在10进制数前面添加一个零,以确保时间格式为24小时制
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
var timeString = years+"-"+months+"-"+days+" "+hours + ":" + minutes + ":" + seconds;
jQuery(".get_current_date_time").html(timeString);
specificDate.setSeconds(currentTime.getSeconds() + 1);
}, 1000);
});