JavaScript ?? ??
? ???? JavaScript Date ??? ?????.
?? ??? ??? ??? ???? ? ?????.
JavaScript ??(??) ?? ????
Date() ???? ???? ?? ??? ???? ??:
<html> <meta charset="utf-8"> <body> <script type="text/javascript"> document.write(Date()) </script> </body> </html>
getTime() 1990? 1? 1??? ????? ??? ?? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p id="demo">單擊按鈕顯示1970年1月1號至今的毫秒數(shù)。</p> <button onclick="myFunction()">點我</button> <script> function myFunction(){ var d = new Date(); var x = document.getElementById("demo"); x.innerHTML=d.getTime(); } </script> </body> </html>
?? ?? ??? ???? ?? setFullYear()? ???? ??:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p id="demo">單擊按鈕顯示修改后的年月日。</p> <button onclick="myFunction()">點我</button> <script> function myFunction(){ var d = new Date(); d.setFullYear(2020,10,3); var x = document.getElementById("demo"); x.innerHTML=d; } </script> <p>記住JavaScript月數(shù)是從0至11。10是11月。</p> </body> </html>
?? ??(UTC? ??)? ???? ???? ?? toUTCString()? ???? ??:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p id="demo">點擊按鈕把utc日期和時間轉換成字符串。</p> <button onclick="myFunction()">點我</button> <script> function myFunction(){ var d = new Date(); var x = document.getElementById("demo"); x.innerHTML=d.toUTCString(); } </script> </body> </html>
??? ?? getDay()? ???? ??? ???? ?? ??? ?? ?:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p id="demo">單擊按鈕顯示今天周幾</p> <button onclick="myFunction()">點我</button> <script> function myFunction(){ var d = new Date(); var weekday=new Array(7); weekday[0]="周日"; weekday[1]="周一"; weekday[2]="周二"; weekday[3]="周三"; weekday[4]="周四"; weekday[5]="周五"; weekday[6]="周六"; var x = document.getElementById("demo"); x.innerHTML=weekday[d.getDay()]; } </script> </body> </html>
? ???? ??? ???? ??:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script> function startTime(){ var today=new Date(); var h=today.getHours(); var m=today.getMinutes(); var s=today.getSeconds();// 在小于10的數(shù)字錢前加一個‘0’ m=checkTime(m); s=checkTime(s); document.getElementById('txt').innerHTML=h+":"+m+":"+s; t=setTimeout(function(){startTime()},500); } function checkTime(i){ if (i<10){ i="0" + i; } return i; } </script> </head> <body onload="startTime()"> <div id="txt"></div> </body> </html>
Creation Date
Date ??? ??? ??? ???? ? ?????.
?? ??? new ???? ?? ??? ? ????. ?? ??? myDate?? Date ??? ?????.
??? ????? ? ?? ??? ????.
new Date() // ?? ?? ? ??
new Date(milliseconds) // 1970? 1? 1?? ??? ?????. ???? ???
new Date(dateString)
new Date(?, ?, ?, ?, ?, ?, ???)
? ????? ???? ???? ?? ?? ???????.
<pFrom 1970="" year="" 1="" Month="" ??? 86,400,000???? ?????<="" p="" style="color: rgb(51, 51, 51 ); ?? ??: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif; ? ??: ??; : rgb(255, 255, 255);">
?? ?????? ? ?? ?:
var today = new Date()
var d1 = new Date("1975? 10? 13? 11:13: 00")
var d2 = new Date(79,5,24)
var d3 = new Date(79,5,24,11,33,0)
?? ??
date ??? ???? ???? Dates? ?????. ?? ??? ? ????.
?? ???? ?? ??? ?? ?? ??(2010? 1? 14?)? ?????.
var myDate=new Date();
myDate.setFullYear(2010,0,14);
???? ????? ?? ??? 5? ?? ??? ??????.
var myDate=new Date();
myDate.setDate(myDate.getDate()+5);
??: ? ?? ??? ? ?? ??? ???? ?? ??? ???? ? ??? ?????.
? ?? ??
Date ??? ???? ? ??? ??? ?? ????.
?? ??? ?? ??? 2100? 1? 14?? ?????.
var x=new Date();
x.setFullYear(2100,0,14);
var today = new Date();
if (x>today)
{
Alert("??? 2100? 1? ?????. 14?");
}
else
{
Alert("??? 2100? 1? 14? ?????.");
}