To get the current date in JavaScript, you can use the built-in Date
object. Here's an example of how to get the current date in JavaScript:
javascriptvar currentDate = new Date();
This creates a new Date
object that represents the current date and time. You can then use the methods of the Date
object to get specific information about the date and time.
For example, you can use the getFullYear()
, getMonth()
, and getDate()
methods to get the current year, month, and day of the month, respectively:
javascriptvar currentDate = new Date();
var year = currentDate.getFullYear();
var month = currentDate.getMonth() + 1; // months are 0-indexed, so add 1
var day = currentDate.getDate();
console.log(year + '-' + month + '-' + day); // Output: "2023-4-9"
This will output the current date in the format "yyyy-mm-dd". Note that the month value is 0-indexed, so you need to add 1 to get the correct month value.
You can also use the getHours()
, getMinutes()
, and getSeconds()
methods to get the current time:
javascriptvar currentDate = new Date();
var hours = currentDate.getHours();
var minutes = currentDate.getMinutes();
var seconds = currentDate.getSeconds();
console.log(hours + ':' + minutes + ':' + seconds); // Output: "12:34:56"
This will output the current time in the format "hh:mm:ss".
Comments
Post a Comment