In this post we are going to see how to get current date and time in javascript. Javascript get current date 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.
Contents
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
Step 2: Javascript Get Current Date Time 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);
Above code will give you the current date as below format (DD/MM/YYYY)
23/9/2017
Posts you may also like
- Change Document / Window Title using jQuery or Javascript
- Detect Browser information using Javascript
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,
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
Nice Explanation & its very useful Karthik
Thanks