How to get current Date and Time using JavaScript

JavaScript Date object provides an easy way to work with dates and times in the client-side script. If you want to get the current date or change the format of a date and time, the JavaScript Date object is very useful.

In this tutorial, we will show you how to get the current date and time in JavaScript. Use the following JavaScript code snippet to get the 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;

1 Comment

  1. Thakur Said...

Leave a reply

keyboard_double_arrow_up