Get Date Using JavaScript

We can get Date using JavaScript built in object Date. In below example we will see how to get date using JavaScript Date object.

example to get current date using JavaScript in dd-mm-yyyy format:-

var today = new Date();
var day = today.getDate();
var month = today.getMonth()+1; //first month starts from 0
var year = today.getFullYear();

if(day<10) {
    day='0'+day
} 

if(month<10) {
    month='0'+month
} 

today = day+'/'+month+'/'+year;
document.write(today);

the above code will print the date in dd/mm/yyyy format. If you want to print the date in different format like mm/dd/yyyy format or mm-dd-yyyy or in any different combination, then we can easily modify above code.

Date object in JavaScript can only be instantiated by calling the constructor of Date object. Date object provides many overloaded Date constructors as shown below

get date using JavaScript

Leave a Comment