How to get current Date and Time using JavaScript


In this tutorial, we will show how you can get the current date and time in JavaScript. Use the following JavaScript code to get current date and time with Y-m-d and H:i:s format.

JavaScript Date object help us to work with dates. Use new Date() to create a new object with current date and time.

var today = new Date();

Get current date from Date object in Y-m-d format.

var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();

Get current time from Date object in H:i:s format.

var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();

Get current date and time from Date object in Y-m-d H:i:s format.

var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;

Leave a reply

Connect With CodexWorld