Current Date in mm/dd/yyyy Format JavaScript

In this tutorial we will learn how to Get Current Date in mm/dd/yyyy Format with JavaScript. JavaScript new Date() Constructor, getDate() method, getMonth() method and getFullYear() method can be used for this.

JavaScript new Date() Constructor

JavaScript new Date() Constructor creates a date object which represents current date and time.

The new Date() Constructor returns the date object which has current time in milliseconds from 1st January 1970 UTC till current date.

JavaScript getDate() method

JavaScript getDate() method returns the current day of the month from 1 to 31.

JavaScript getMonth() method

JavaScript getMonth() method returns the current month of the year, the values range from 0 to 11, where 0 represents the first month and so on. That is why we add 1 to the value of month to display the accurate month of current date.

JavaScript getFullYear() method

JavaScript getFullYear() method returns the year of the current date in four digit format from 0000 to 9999.

HTML Code

HTML Code is given below, in this code we have a paragraph tag to display the current date in mm/dd/yyyy format while a Button tag is used to execute a JavaScript function.

<p id="output"></p>
<button onclick="getDate()">Get Date</button>    

JavaScript Code

Take a look at the JavaScript code.

<script>
function getDate()
{
var date = new Date();
var day = date.getDate();
var month = date.getMonth()+1; 
var year = date.getFullYear();
if(day<10) 
{
    day='0'+day;
} 
if(month<10) 
{
    month='0'+month;
} 
var currentDate = month + '/' + day + '/' + year;    
document.getElementById('output').innerHTML = currentDate;
}
</script>

Demo

Change font-size using JavaScript Get Font Size of any Tag using JavaScript Change Position of HTML Element using JavaScript How to Change Image on Hover with JavaScript How to Disable Button by Class Name in JavaScript How To Change Image Size with JavaScript How to change Image opacity with JavaScript How to Change image src on click with JavaScript How to get the lang attribute value in JavaScript How to Get attribute Value using JavaScript How To Check if Attribute Exists or not with JavaScript How To Count number of links on Page with JavaScript How To Scroll Page to Bottom using JavaScript How To Detect Shift Key Press with JavaScript Change Text Color On Hover with JavaScript Hide and Show div using JavaScript Get Button text with JavaScript Get textarea value with JavaScript Get table row Height with JavaScript Auto Increase width of input field with JavaScript Set Textarea maxLength with JavaScript Set Textarea Value with JavaScript JavaScript Count list items JavaScript set input field value Count Button Clicks with JavaScript Scroll Page to the Top on Click with JavaScript