The Date object is a built-in object in JavaScript that can be used to handle dates and times, including days, months, years, hours, minutes, seconds and milliseconds. We will use new Date() to create a date object. After creating a Date object, you can use many methods to operate it. In this article, we will take a closer look at how to use the Date object.
Let’s first use the new keyword to create an instance of a Date object
var my_date = new Date() var my_date = new Date(milliseconds); var my_date = new Date('date string'); var my_date = new Date(year, month[, date, hour, minute, second, millisecond]);
After creating an instance of the Date object, you can All methods of this object are accessed from the "my_date" variable.
The following parameters can be specified in the Date constructor
No parameters: If no parameters are specified in the constructor, the Date object will be set to the current date and time.
Milliseconds: Milliseconds can be specified as a numeric parameter. The date object will calculate the date and time by adding the specified number of milliseconds since noon on January 1, 1970
Date string: The string argument will be treated as a date and will be parsed using the Date.parse method .
The date of the Date parameter includes the following seven parameters:
year: A value representing the year of the date.
month: A numeric value representing the date and month. Starting from January 0 to December 11
date: A numeric value representing the date (optional)
hour: A numeric value representing the hour of the day (optional).
minute: A value representing minutes (optional).
second: A numerical value representing seconds (optional)
millisecond: A numerical value representing milliseconds (optional)
Let’s look at a specific example
Date:
<html> <body> <script type="text/javascript"> var d = new Date() document.write(d.getDate()) document.write(".") document.write(d.getMonth() + 1) document.write(".") document.write(d.getFullYear()) </script> </body> </html>
The current date displayed on the browser is: 2019.1.30
Time:
<html> <body> <script type="text/javascript"> var d = new Date() document.write(d.getHours()) document.write(":") document.write(d.getMinutes() + 1) document.write(":") document.write(d.getSeconds()) </script> </body> </html>
The current time displayed on the browser is: 14:40:43
You can also use setDate, setHour, etc. to set the date or time as a date object.
This article ends here. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !
The above is the detailed content of How to use Date object. For more information, please follow other related articles on the PHP Chinese website!