JavaScript Date
Date objects are used to handle dates and times.
Online example
How to use the Date() method to get the date of today.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> var d=new Date(); document.write(d); </script> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
getFullYear()Use getFullYear() to get the year.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo">点击按钮获取今年的年份。</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var d = new Date(); var x = document.getElementById("demo"); x.innerHTML=d.getFullYear(); } </script> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
getTime()
getTime() Returns the number of milliseconds since January 1, 1970.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo">单击按钮显示1970年1月1号至今的毫秒数。</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var d = new Date(); var x = document.getElementById("demo"); x.innerHTML=d.getTime(); } </script> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
setFullYear()
How to use setFullYear() to set a specific date.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(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月数是从0至11。10是11月。</p> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
toUTCString()
How to use toUTCString() to convert today's date (according to UTC) into a string.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(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>
Run instance»
Click the "Run instance" button to view the online instance
getDay()
How to use getDay() with an array to display the day of the week, not just the number.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(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>
Run instance»
Click the "Run instance" button to view the online instance
Display a clock
How to display a clock on a web page.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script> function startTime(){ var today=new Date(); var h=today.getHours(); var m=today.getMinutes(); var s=today.getSeconds();// 在小于10的数字钱前加一个‘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>
Run instance »
Click the "Run instance" button to view the online instance
Complete Date Object Reference Manual
We provide JavaScript Date objects Reference manual, which includes all properties and methods available for date objects. JavaScript Date Object Reference Manual.
This manual contains detailed descriptions and related examples of each property and method.
Creation Date
Date object is used to process dates and time.
Date objects can be defined through the new keyword. The following code defines a Date object named myDate:
There are four ways to initialize the date:
new Date(milliseconds ) //Returns the number of milliseconds from January 1, 1970 to the present
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
Most of the above parameters are optional. If not specified, the default parameter is 0.
Some examples of instantiating a date:
var d1 = new Date("October 13, 1975 11:13:00" )
var d2 = new Date(79,5,24)
var d3 = new Date(79,5,24,11,33,0)
Set Date
By using methods on date objects, we can easily operate on dates.
In the following example, we set a specific date (January 14, 2010) for the date object:
Comparison of two dates
var today = new Date();
if (x>today)
{
alert("Today is before January 14, 2100");
}
else
{
alert("Today is after January 14, 2100");
}