How to get Current Time and date in Javascript


Read {count} times since 2020

This tutorial will help you to get current date and time at which the function is executed. Here is the function:

function ctime(){
var currentdate = new Date();
var time=currentdate.getFullYear()+"-"+(currentdate.getMonth()+1)+"-"+currentdate.getDate()+" "+currentdate.getHours()+":"+currentdate.getMinutes()+":"+currentdate.getSeconds();
return time;
}

Just print out ctime() function to get the date and time in the format YEAR/MONTH/DAY HOUR:MINUTE:SECOND 

Explanation :

new Date() function prints out the current date and time in a disorderly way. You can order it in any way as I did in the time() function.
currentdate.getFullYear() will get the year from new Date() which is in the variable currentdate
currentdate.getMonth() will get the month from new Date() function. The program adds 1 to the month because Javascript always count the month from . So we should add a +1.
currentdate.getDate() will get the day of the month in which the function is executed.
currentdate.getHours() will give the Hour of the time.
currentdate.getMinutes() will give the Minute in which the script is executed.
currentdate.getSeconds() This function will give the seconds of the time at which the script is executed.
Show Comments