add date in javascript
Adding a date in Javascript is a fairly simple process. All you need to do is create a Date object and use the .getDate()
method to get the day of the month. This method will return the day of the month as an integer from 1-31.
let currentDate = new Date();
let dayOfMonth = currentDate.getDate();
console.log("Day of the month is " + dayOfMonth);
You can also use the .getMonth()
and .getFullYear()
methods to get the month and year respectively. This method will return the month as an integer from 0-11 and the year as an integer from 0-9999.
let currentDate = new Date();
let month = currentDate.getMonth();
let year = currentDate.getFullYear();
console.log("Month is " + month + " and year is " + year);
You can also use the .toDateString()
method to get a string representation of a date. This method will return the date in the format of "Day, Month Date, Year".
let currentDate = new Date();
let dateString = currentDate.toDateString();
console.log("Current date is " + dateString);
Finally, you can use the .toLocaleDateString()
method to get a localized representation of a date. This method will return the date in the format specified by the user's locale settings.
let currentDate = new Date();
let localizedDateString = currentDate.toLocaleDateString();
console.log("Localized date is " + localizedDateString);