How can I get dd/mm/yyyy formatted current date in Javascipt?


I am trying to get today's date using Javscript, which I can do using new Date() but i would like to know, how can I get it formatted in dd/mm/yyyy and show it in datepicker input?


Asked by:- bhanu
0
: 164 At:- 1/23/2020 2:53:42 PM
Javascript dd/mm/yyyy formatted current date






1 Answers
profileImage Answered by:- vikas_jk

You can create a function to get Current date formatted in javascript as below

function GetTodaysdate()
{ 
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth() + 1; //January is 0!

    var yyyy = today.getFullYear();
    if (dd < 10) {
      dd = '0' + dd;
    } 
    if (mm < 10) {
      mm = '0' + mm;
    } 
    
    return dd + '/' + mm + '/' + yyyy;
}

document.getElementById("DateValue").value=  GetTodaysdate();

HTML

<input type="text" id="DateValue" />

Output:

get-current-date-formatted-ddmmyyyy-javascript-min.png

Fiddle Link

OR

Simply use .toJSON.ToSlice

<input type="text" id="CurrentDate"/>
<script>document.getElementById("CurrentDate").value = new Date().toJSON().slice(0,10).split('-').reverse().join('/')</script>

Fiddle

OR

You can use moment.js also

     // set current value in textbox with id CurrentDate, using moment.js
     $("#CurrentDate").val( moment().format('MMM D, YYYY') );

Any of the above method is good, second method looks quick and easy.

1
At:- 1/24/2020 11:15:00 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly