How to Get the Current Date in JavaScript

The Date object in JavaScript represents a date and time, internally represented as the number of milliseconds elapsed since the epoch - midnight on January 1, 1970, UTC. JavaScript usually utilizes the time zone of the browser, displaying dates in the local time zone by default. When displayed, the date is typically shown in a full text string format, making it human-readable.

Date objects in JavaScript

JavaScript's Date objects are generated using the new Date() constructor. This function instantiates a new date object that corresponds to the current date and time. The Date object offers a variety of methods to manipulate and extract information from dates. The particular method you utilize will depend on the desired format of the date information you intend to obtain. This flexibility allows you to work with dates in diverse ways, accommodating different use cases and formatting requirements.

Simple JavaScript Date operation

var today = new Date(); document.getElementById("demo").innerHTML = "Today is: " + today;
run this source code Browser View

If you want to get Day, Month and Year separate then you can use the following code.

var currentDate = new Date() var day = currentDate.getDate() var month = currentDate.getMonth() + 1 var year = currentDate.getFullYear() document.getElementById("demo").innerHTML = "Today is: " + day + "/" + month + "/" + year;
run this source code Browser View


todays date inJavascript

Using Date.prototype.toLocaleDateString()

The toLocaleDateString() method is a built-in function in JavaScript's Date object that returns a string presenting the date part of the specified date, utilizing the local language-sensitive formatting rules. This method also takes into account the user agent's time zone, ensuring the representation of the date is appropriate for the user's region.

today = new Date().toLocaleDateString() document.getElementById("demo").innerHTML = "Today is: " + today;
run this source code Browser View

Using Date.prototype.toISOString()

The toISOString() method in JavaScript's Date object returns the date as a string, adhering to the ISO-8601 standard format. This format is represented as "YYYY-MM-DDTHH:mm:ss.sssZ," where:

  1. YYYY represents the year.
  2. MM represents the month.
  3. DD represents the day.
  4. THH represents the hour.
  5. mm represents the minute.
  6. ss represents the second.
  7. sss represents the milliseconds.
  8. Z represents the UTC time zone.

The toISOString() method produces a standardized string representation that's commonly used for data interchange and storage across different systems.

today = new Date().toISOString().slice(0, 10) document.getElementById("demo").innerHTML = "Today is: " + today;
run this source code Browser View

Detailed date format

You can try this detailed date format like day name , month name etc.

run this source code Browser View

Full Source | JavaScript
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> var objToday = new Date(), weekday = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'), dayOfWeek = weekday[objToday.getDay()], domEnder = function() { var a = objToday; if (/1/.test(parseInt((a + "").charAt(0)))) return "th"; a = parseInt((a + "").charAt(1)); return 1 == a ? "st" : 2 == a ? "nd" : 3 == a ? "rd" : "th" }(), dayOfMonth = today + ( objToday.getDate() < 10) ? '0' + objToday.getDate() + domEnder : objToday.getDate() + domEnder, months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'), curMonth = months[objToday.getMonth()], curYear = objToday.getFullYear(); var today = dayOfWeek + " " + dayOfMonth + " of " + curMonth + ", " + curYear; document.getElementById("demo").innerHTML = "Today is: " + today; </script> </body> </html>

Finally, you can use a single line code using JSON for getting the current date.

today = new Date().toJSON().slice(0,10).replace(/-/g,'/'); document.getElementById("demo-6").innerHTML = "Today is: " + today;
run this source code Browser View

Moreover, you can use Date.js library which extends Date object , thus you can have .today() method.

Conclusion

To acquire the current date in JavaScript, create a Date object without parameters, and then use the object's methods to extract the required components. This enables you to work with and display the current date and time effectively.