效果:
<div class="nowTime"> <span></span>年 <span></span>月 <span></span>日 <span></span>: <span></span>: <span></span> </div>
<script> $(document).ready(function() { function time() {
var date = new Date();
var n = date.getFullYear();
var y = date.getMonth() + 1;
var t = date.getDate();
var h = (date.getHours()<10)?"0"+date.getHours():date.getHours();
var m = (date.getMinutes()<10)?"0"+date.getMinutes():date.getMinutes();
var s = (date.getSeconds()<10)?"0"+date.getSeconds():date.getSeconds();
$('.nowTime span').eq(0).html(n); $('.nowTime span').eq(1).html(y); $('.nowTime span').eq(2).html(t); $('.nowTime span').eq(3).html(h); $('.nowTime span').eq(4).html(m); $('.nowTime span').eq(5).html(s); for (var i = 0; i < $('div').length; i++) { if ($('div').eq(i).text().length == 1) { $('div').eq(i).html(function(index, html) { return 0 + html; }); } } } time(); setInterval(time, 1000); }); <script>