Site icon webarchers

How to change date format in JavaScript

change date format in javascript

I can see what brings you here. So, if you want the answer of how to change date format in JavaScript, you are at right place. In this tutorial you will find the code to manipulate the date format in JavaScript. Here you will find the various code which convert dd/mm/yyyy to mm/dd/yyyy in JavaScript, or give you the date format yyyy-mm-dd hh:mm:ss, or beautify the ugly numbers to good looking and readable date.

Let’s write some code then:

Change Date Format yyyy-mm-dd hh:mm:ss in JavaScript

The code below will bring you the result formatted in yyyy-mm-dd hh:mm:ss

function convert(){
  let current_datetime = new Date()
  let formatted_date = current_datetime.getFullYear() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getDate() + " " + current_datetime.getHours() + ":" + current_datetime.getMinutes() + ":" + current_datetime.getSeconds();
  return formatted_date;
}

Convert dd/mm/yyyy to mm/dd/yyyy in JavaScript

If you got a date already in dd/mm/yyyy format…then you need to do this to convert it to mm/dd/yyyy:

function convert(date){
  var datearray = date.split("/");
  var newdate = datearray[1] + '/' + datearray[0] + '/' + datearray[2];
  return newdate;
  
}


Beautify date yyyy-mm-dd hh:mm:ss in month name and day name format

If you want to format a given date in more user friendly by changing the month number to month name:

<p id="demo"></p>
var x= changeDate('2020-04-13 12:15:28.00');

document.getElementById("demo").innerHTML = x;

 function changeDate(date){
    let currentDate = new Date(date);
    var fd = currentDate.toDateString();
    return fd;
  }
// expected output Mon Apr 13 2020

Okay guys, in this tutorial I shared the code which can easily change the date format in JavaScript with ease. You can use Moment.js to make date time format change and conversions easier.

Please let me know in the comment section if you have any question.

You may find this helpful:

PHP Scripts to sending mail in PHP

Exit mobile version