簡單系統時間
var nowTime = new Date();//typeof object
下面出來的都數字類型:number
nowTime.getFullYear() //年
nowTime.getMonth() + 1 //月 從0開始算,所以需加1
nowTime.getDate() //日
nowTime.getDay() //星期
nowTime.getHours() //時
nowTime.getMinutes() //分
nowTime.getSeconds() //秒
Time()
setInterval(Time, 1000)
function Time() {
var nowTime = new Date(); //typeof object
var _year = nowTime.getFullYear();
var _month = nowTime.getMonth() + 1;
var _date = nowTime.getDate();
var _week = nowTime.getDay();
var _hours = nowTime.getHours()
var _minutes = nowTime.getMinutes();
var _seconds = nowTime.getSeconds();
switch (_week){
case 0:_week='星期日';break;
case 1:_week='星期一';break;
case 2:_week='星期二';break;
case 3:_week='星期三';break;
case 4:_week='星期四';break;
case 5:_week='星期五';break;
case 6:_week='星期六';
}
document.body.innerHTML = _year + '年' + _month + '月' + _date + '日 ' + _week + ' ' + toTwo(_hours) + ':' + toTwo(_minutes) + ':' + toTwo(_seconds);
}
function toTwo(n) { //單數轉雙數
return n = n < 10 ? '0' + n : '' + n;
}
圖片日期要點:日期共6位,使用str.charAt(i)獲取每一位數字,與圖片名字相對應
str = toTwo(_hours)+toTwo(_minutes)+toTwo(_seconds);
for(var i=0;i<str.length;i++){
img[i].src = '../img/timePic/'+ str.charAt(i)+'.JPG'
}
倒計時:
設置時間形式:
數字形式:new Date(2013,6,22,01,19,54)
字符串形式:new Date('June 10,2013 12:01:20')
思想:(未來時間 - 當前時間)/1000 = t //秒
套用
天:Math.floor(t/86400)
時:Math.floor(t%86400/3600)
分:Math.floor(t%86400%3600/60)
秒:t%60
Math.floor(t/86400)+'天'+ Math.floor(t%86400/3600)+'時'+ Math.floor(t%86400%3600/60)+'分'+t%60+'秒'
一月:January 简写Jan.
二月:February 简写Feb.
三月:March 简写Mar.
四月:April 简写Apr.
五月:May 简写May.
六月:June 简写Jun.
七月:July 简写Jul.
八月:August 简写Aug.
九月:September 简写Sep. / Sept.
十月:October 简写Oct.
十一月:November 简写Nov.
十二月:December 简写Dec.
開始時間:<input type="text" value="August 29,2015 1:39:59"/>
剩余時間:<input type="text"/>
<input type="button" value="Start"/>
var nowTime = new Date();
//var newTime = new Date(2013, 5, 22, 1, 39, 59);
var newTimeE = new Date('August 29,2014 1:39:59')
var t = Math.floor((newTimeE - nowTime) / 1000); //newTime - nowTime 毫秒單位 t為秒
var str = Math.floor(t / 86400) + '天' + Math.floor(t % 86400 / 3600) + '時' + Math.floor(t % 86400 % 3600 / 60) + '分' + t % 60 + '秒'
var inp = document.getElementsByTagName('input')
var inow = null;
var inew = null;
var timer = null;
inp[2].onclick = function () {
clearInterval(timer)
inew = new Date(inp[0].value)
timer = setInterval(function () {
inow = new Date();
var tt = Math.floor((inew - inow) / 1000);
if (tt >= 0) {
var str = Math.floor(tt / 86400) + '天' + Math.floor(tt % 86400 / 3600) + '時' + Math.floor(tt % 86400 % 3600 / 60) + '分' + tt % 60 + '秒'
inp[1].value = str;
}
else { //倒計時完成後動作
clearInterval(timer)
}
}, 1000)
}
new Date().getTime() //時間戳:1970年1月1日0點0分0秒0毫秒 距離現在多長時間,毫秒為單位