JavaScript Date object
This chapter introduces the JavaScript Date (date) object.
Date object is used to handle date and time.
JavaScript Date (date) object instance
How to use the Date() method to get the date of the day:
<html> <meta charset="utf-8"> <body> <script type="text/javascript"> document.write(Date()) </script> </body> </html>
getTime() returns the date from January 1, 1990 to the present Number of milliseconds:
<!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>
How to use setFullYear() to set a specific date:
<!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>
How to use toUTCString() to convert today’s date (according to UTC) into a string:
<!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>
How to use getDay() and an array to display the day of the week, not just numbers:
<!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>
How to display a clock on a web page:
<!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>
Creation date
Date objects are used to handle dates and times.
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() // Current date and time
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)
The above parameters are large Most are optional and default to 0 if not specified.
<pFrom 1970="" year="" 1="" month="" a day is calculated as 86,400,000 milliseconds<="" p="" style="color: rgb(51 , 51, 51); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif; font-size: 12px; line-height: normal; white-space : normal; background-color: rgb(255, 255, 255);">
Some examples of instantiating a date:
var today = new 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 the 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:
var myDate=new Date();
myDate. setFullYear(2010,0,14);
In the following example, we set the date object to the date 5 days later:
var myDate=new Date();
myDate .setDate(myDate.getDate()+5);
Note: If adding days will change the month or year, then the date object will automatically complete this conversion.
Comparison of two dates
Date objects can also be used to compare two dates.
The following code compares the current date to January 14, 2100:
var x=new Date();
x.setFullYear(2100,0,14);
var today = new Date();
if (x>today)
{
alert("Today is before January 14, 2100");
}
else
{
alert("Today is after January 14, 2100");
}