How to get current formatted date dd/mm/yyyy in Javascript and append it to an input?
Method 1: Using toLocaleDateString() method: The toLocaleDateString() method is used to return a language sensitive representation of the date part of a Date object as a String. It has two optional parameters, locales and options. The locales parameter can be specified with language tags that are used to display the date according to the format of that locale. The locale has various options that can be used to modify the locale used.
The date can be formatted as ‘dd/mm/yyyy’ by specifying the locale as ‘en-GB’ which sets the locale to ‘British English’.
The date can be set to an input using the valueAsDate property. This property takes in a Date object to be displayed in the date selector by default. The current date can be shown by assigning it a new Date() object.
Syntax:
new Date().toLocaleDateString('en-GB')
Example:
<!DOCTYPE html> < html > < head > < title > How to get current formatted date dd/mm/yyyy in Javascript and append it to an input? </ title > </ head > < body > < h1 style = "color: green" > GeeksforGeeks </ h1 > < b > How to get current formatted date dd/mm/yyyy in Javascript and append it to an input? </ b > < p > Current Date is: < span class = "currDate" ></ span > </ p > < p > Please select the date to start the course: < input type = "date" id = "dateInput" > </ p > < script type = "text/javascript" > let currDate = new Date().toLocaleDateString('en-GB'); let inputDate = new Date(); document.querySelector('.currDate').textContent = currDate; document.querySelector('#dateInput').valueAsDate = inputDate; </ script > </ body > </ html > |
Output:
Method 2: Splicing the string from the toLocalISOString() method: The date can be extracted from the string returned by the toLocalISOString() method. The string returned is sliced from 0 to 10th index by using slice(0, 10), split into separate components by the dash(-) separator, reversed to make the day value first, and then joined back using slash(/) as the separator. This gives the date in the ‘dd/mm/yyyy’ format.
The date can be set to an input using the valueAsDate property. This property takes in a Date object to be displayed in the date selector by default. The current date can be shown by assigning it a new Date() object.
Syntax:
new Date().toISOString().slice(0, 10).split('-').reverse().join('/')
Example:
<!DOCTYPE html> < html > < head > < title > How to get current formatted date dd/mm/yyyy in Javascript and append it to an input? </ title > </ head > < body > < h1 style = "color: green" > GeeksforGeeks </ h1 > < b > How to get current formatted date dd/mm/yyyy in Javascript and append it to an input? </ b > < p > Current Date is: < span class = "currDate" ></ span > </ p > < p > Please select the date to start the course: < input type = "date" id = "dateInput" > </ p > < script type = "text/javascript" > let currDate = new Date().toISOString().slice(0, 10) .split('-').reverse().join('/'); let inputDate = new Date(); document.querySelector('.currDate').textContent = currDate; document.querySelector('#dateInput').valueAsDate = inputDate; </ script > </ body > </ html > |
Output:
Recommended Posts:
- How to get current formatted date dd/mm/yyyy in JavaScript ?
- How to get the current date in JavaScript ?
- How to get the first and last date of current month using JavaScript ?
- How to get the current date and time in seconds?
- How to append an element in an array in JavaScript?
- How to append HTML code to a div using JavaScript ?
- How to append data to <div> element using JavaScript ?
- Implement prepend and append with regular JavaScript
- Print current day and time using HTML and JavaScript
- How to close current tab in a browser window using JavaScript?
- HTML | <input type="date">
- HTML | DOM Input Date name Property
- HTML | DOM Input Date value Property
- HTML | DOM Input Date min Property
- HTML | DOM Input Date max Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.