In this post we are going to see how to get current date and time in javascript. Javascript Current Date and time is useful to validate date within your client side application and also used to  greet users  like facebook such as Good Morning! Have a great day, Good Afternoon and Good Night Messages.

This simple Current Date and Time in Javascript function is used to get client’s computer current date and time. let us see how to parse and format those dates in javascript below.

Current Date and Time in Javascript
Javascript Current Date and Time

Step 1: Create an Object Date

Date object contains year, month, day, hour, minute, seconds and milliseconds. The below code will creates a new date object with the current date and time

var today = new Date(); //date object

When you print this variable  today , It will give the following output

 Sat Sep 23 2017 11:35:59 GMT+0530 (India Standard Time) 

 

Step 2: Getting Javascript Current Date from Date Object

Date object has different methods to parse the date from date object. You can get the Date, Month and Year  from  getDate()  getMonth() ,  getYear() methods.


var today = new Date();
var current_date = today.getDate();
var current_month = today.getMonth()+1; //Month starts from 0
var current_year = today.getFullYear();
alert(current_date+"/"+current_month+"/"+current_year);

 

 getMonth()   – you need to add +1 to display the correct month because javascript month starts from 0.

Above code will give you the current date as below format (DD/MM/YYYY)

 23/9/2017 

 

Posts you may also like

Step 3: Getting Javascript Current Time from Time Methods

For getting current time, I have used prototype constructor for the date object.  Date.prototype.getCurrentTime   – getCurrentTime function will be inherited by all the instances of Date Object.


Date.prototype.getCurrentTime = function(){
return ((this.getHours() < 10)?"0":"") + ((this.getHours()>12)?(this.getHours()-12):this.getHours()) +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds() + ((this.getHours()>12)?(' PM'):' AM');
};

Below code is used to get the current time


var current_time = today.getCurrentTime();

output of the above code will be

 12:15:09 AM 

 

Lets combine all the above codes and will give you the javascript current date and time using date object

Final Code to get Current Date and Time in Javascript


Date.prototype.getCurrentTime = function(){
return ((this.getHours() < 10)?"0":"") + ((this.getHours()>12)?(this.getHours()-12):this.getHours()) +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds() + ((this.getHours()>12)?(' PM'):' AM');
};

var today = new Date(); //date object
var current_date = today.getDate();
var current_month = today.getMonth()+1; //Month starts from 0
var current_year = today.getFullYear();
var current_time = today.getCurrentTime();
alert(current_date+"/"+current_month+"/"+current_year+' - '+current_time);

Above code will give the exact system’s current date and time using javascript date object

Output,

 23/9/2017 – 12:20:46 AM 

 

Conclusion

For futher improvement, You can use javascript’s  setInterval  function to update the javascript current date and time in realtime. Please check the live demo for realtime

 

 

 

Related Posts