Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 知乎專欄 | 視頻教程 | About

11.5. Date and Time

var dateObject=new Date();
document.writeln(dateObject.toDateString());
Mon Mar 28 2016

document.writeln(dateObject.toLocaleDateString());
‎2016‎年‎3‎月‎28‎日

document.writeln(dateObject.toISOString())
2016-03-28T08:57:30.244Z

document.writeln(dateObject.toISOString().slice(0,10));
2016-03-28

document.writeln(dateObject.toISOString().slice(11,19));
09:11:12

document.writeln(dateObject.toTimeString().slice(0,9));
17:16:11

document.writeln(new Date("2016-3-30").getTime());
1459267200000
		
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
		

例 11.1. 倒數計時例子

			
function checkTime(i)    
{    
   if (i < 10) {    
       i = "0" + i;    
    }    
   return i;    
}
			
var ts = (new Date("2016-3-30")) - (new Date());//計算剩餘的毫秒數  
var dd = parseInt(ts / 1000 / 60 / 60 / 24, 10);//計算剩餘的天數  
var hh = parseInt(ts / 1000 / 60 / 60 % 24, 10);//計算剩餘的小時數  
var mm = parseInt(ts / 1000 / 60 % 60, 10);//計算剩餘的分鐘數  
var ss = parseInt(ts / 1000 % 60, 10);//計算剩餘的秒數  
day = checkTime(dd);  
hour = checkTime(hh);  
minute = checkTime(mm);  
second = checkTime(ss);  

document.writeln(day + "天" + hour + "時" + minute + "分" + second + "秒");