In this tutorial we will learn how to Get Current Date in dd/mm/yyyy Format in JavaScript. JavaScript new Date() Constructor, getDate() method, getMonth() method and getFullYear() method can be used for this.
Table of Contents
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 dd/mm/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 = day + '/' + month + '/' + year;
document.getElementById('output').innerHTML = currentDate;
}
</script>