[Solved]-How to format Date Object in MM/DD/YYYY HH:MM:SS format using JavaScript ?

[Solved]-How to format Date Object in MM/DD/YYYY HH:MM:SS format using JavaScript ?

If you are looking for the JavaScript code for converting the format current date to MM/dd/yyyy HH:mm:ss format then you have come to the right place.

function formatDate(date) {
                                  var d = new Date(date),
                                      month = '' + (d.getMonth() + 1),
                                      day = '' + d.getDate(),
                                      year = d.getFullYear();
                                      hour = '' + d.getHours();
                                      min = '' + d.getMinutes();
                                  if (month.length < 2) month = '0' + month;
                                  if (day.length < 2) day = '0' + day;
                                  if (hour.length < 2) hour = '0' + hour;
                                  if (min.length < 2) min = '0' + min;
                                  var result = [year, month, day].join('-');
                                return result + " " + hour + ":" + min;
                              }

In the above Javascript function, you just need to pass the date object and it will return the desired date format.

As you can see in the code it’s very simple code you can always format date by extracting the parts of the date object and combine them using string.In above.

You can also write a generic function so that you can convert the date object to any format like below function.

function formatDate(datetimeObj, format) {
        var curr_date = datetimeObj.getDate();
        var curr_month = datetimeObj.getMonth();
        curr_month = curr_month + 1;
        var curr_year = datetimeObj.getFullYear();
        var curr_min = datetimeObj.getMinutes();
        var curr_hr = datetimeObj.getHours();
        var curr_sc = datetimeObj.getSeconds();
        if (curr_month.toString().length == 1)
            curr_month = '0' + curr_month;
        if (curr_date.toString().length == 1)
            curr_date = '0' + curr_date;
        if (curr_hr.toString().length == 1)
            curr_hr = '0' + curr_hr;
        if (curr_min.toString().length == 1)
            curr_min = '0' + curr_min;

        if (format == 1)//dd-mm-yyyy
        {
            return curr_date + "-" + curr_month + "-" + curr_year;
        }
        else if (format == 2)//yyyy-mm-dd
        {
            return curr_year + "-" + curr_month + "-" + curr_date;
        }
        else if (format == 3)//dd/mm/yyyy
        {
            return curr_date + "/" + curr_month + "/" + curr_year;
        }
        else if (format == 4)// MM/dd/yyyy HH:mm:ss
        {
            return curr_month + "/" + curr_date + "/" + curr_year + " " + curr_hr + ":" + curr_min + ":" + curr_sc;
        }
    }

Ashok Patel

I'm an electronic engineer working in a multinational company,having good experience on Electronics and electrical engineers design and oversee production of electronic equipment such as radios, televisions, computers, washing machines and telecommunication systems.I like to do RND and Research.I also have hands on experience graphic design software and in web designing having great command on ASP.NET, HTML5, JavaScript, T-SQL, JQuery.

Add comment

Donate for Corona Victim

Corona Virus Relief Fund

Your Header Sidebar area is currently empty. Hurry up and add some widgets.