We can use JavaScript to format the date in mm dd yyyy by making use of the JavaScript toLocaleString() method. There are many configurations we can utilize with the toLocaleString() method to display the date how we want. One of these configurations will get the format we want.
Note that we will use the toLocaleDateString() method is our examples below. This method works the same way as the toLocaleString() method, except that it will just display the date, and NOT the time.
Here is the code to get the date in the format of mm dd yyyy. We will explain how we got this below.
var currDate = new Date();
var localDate1 = currDate.toLocaleDateString('en-US', {month: '2-digit', day: '2-digit', year: 'numeric'});
console.log(localDate1);
#Output
08/31/2022
The JavaScript toLocaleString method will take a date object and return the date as a string using the local settings from your computer.
var currDate = new Date();
var localDate = currDate.toLocaleString();
The date is returned in a much friendlier format for the user than what the Date() method returns. The outputs of currDate and localDate from above are displayed as follows:
localDate: 1/28/2022, 9:35:02 PM
So if we wanted to display the date in the mm dd yyyy format, we can use different options for parameters in the toLocaleString() method. A list of these options can be found here.
So to get the date in the format of mm/dd/yyyy, we can simply use the US English language format as a parameter in the toLocaleString() method, and set the month, day, and year formats to be how we want. Let’s see this below.
var currDate = new Date();
var localDate1 = currDate.toLocaleDateString('en-US', {month: '2-digit', day: '2-digit', year: 'numeric'});
console.log(localDate1);
#Output
08/31/2022
If we want to display the date with hyphens instead of slashes, mm-dd-yyyy, we can do this in a slightly different way.
To display the date in the mm-dd-yyyy format we simply get the year, month, and date in the format we want, and combine them. Here is some code below to show how easily this can be done.
var currDate = new Date();
var year = currDate.toLocaleString('en-US', { year: 'numeric'});
var month = currDate.toLocaleString('en-US', { month: '2-digit'});
var day = currDate.toLocaleString('en-US', { day: '2-digit'});
var dateInMMDDYYYY = month + "-" + day + "-" + year;
console.log(dateInMMDDYYYY);
#Output
08-31-2022
Displaying the Current Date and Time in MM DD YYYY using the JavaScript toLocaleString Method
Below we will provide code to get the current date in JavaScript, and let the user see it in a friendly format when they click a button.
<div id="div1">
<div id="click-me" onclick="genNewDate()">Get Date</div>
<p>Today's Date is: <span id="theDate"></span></p>
</div>
We will simply get the Date with the Date() method, and use the toLocaleString() method and some parameter options to display the date and time nicely. Here is the simple JavaScript code:
function genNewDate(){
var currDate = new Date();
var localDate = currDate.toLocaleDateString('en-US', {month: '2-digit', day: '2-digit', year: 'numeric'});
document.getElementById("theDate").innerHTML = localDate;
}
The final code and output for this example is below:
Code Output:
Today’s Date is:
Full Code:
<div id="div1">
<div id="click-me" onclick="genNewDate()">Get Date</div>
<p>Today's Date is: <span id="theDate"></span></p>
</div>
<script>
function genNewDate(){
var currDate = new Date();
var localDate = currDate.toLocaleDateString('en-US', {month: '2-digit', day: '2-digit', year: 'numeric'});
document.getElementById("theDate").innerHTML = localDate;
}
</script>
Hopefully this article has been useful in helping you understand how to use JavaScript to format the date as mm dd yyyy.
Leave a Reply