js定时器实时获取当前时间
实时获取当前时间,加个定时器,就可以按需索取,获取时间了!
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>获取当前时间</title>
</head>
<body>
<div class="time"></div>
<script type="text/javascript" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$(function(){
timeNow();
function timeNow(){
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
var hour = date.getHours();
var minu =date.getMinutes();
var sec =date.getSeconds();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (minu >= 0 && minu <= 9) {
minu = "0" + minu;
}
if (sec >= 0 && sec <= 9) {
sec = "0" + sec;
}
if (hour >= 0 && hour <= 9) {
hour = "0" + hour;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + hour + seperator2 + minu + seperator2 + sec;
$('.time').html('年月日:'+date.getFullYear() + seperator1 + month + seperator1 + strDate +'时分秒:'+hour + seperator2 + minu +':'+sec );
};
var time = null;
time=setInterval(timeNow,1000);
});
</script>
</body>
</html>
点击查看更多内容
29人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦