To get the current date in JavaScript, you can use the following methods: Date.now() Returns the number of milliseconds that have elapsed since January 1, 1970. new Date() returns a Date object representing the current moment. The following methods can be used to obtain the date component: getFullYear(), getMonth(), getDate(), getHours(), getMinutes(), getSeconds(). You can use the following methods to format dates: toLocaleDateString(), toLocaleTimeString(), toISOSt
##How to get the current date in JavaScript
In JavaScript, you can get the current date through the following methods:1. Date.now()
Date.now() Returns the number of milliseconds elapsed from midnight on January 1, 1970 to the current moment. You can get the date object using the following method:
<code class="js">const date = new Date(Date.now());</code>
2. new Date()
new Date() Returns a date representing the current moment Date object. You can get the date object in the following ways:
<code class="js">const date = new Date();</code>
Get the date components
Get the components of the date object (such as year, month, day, hour, minute, second) , you can use the following methods:: Get the year
: Get the month (0-11)
: Get the date (1-31)
: Get the hour (0-23)
: Get minutes (0-59)
: Get seconds (0-59)
<code class="js">const year = date.getFullYear();</code>
Format date
To format the date, you can use the following method:: Returns a localized date string
: Returns a localized time string
: Returns an ISO 8601 date string
<code class="js">const formattedDate = date.toLocaleDateString();</code>
The above is the detailed content of How to get the current date in js. For more information, please follow other related articles on the PHP Chinese website!