How to get current date and time (yyyy-mm-dd H:i:s) using javascript

javascript
date
time

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 = new Date();
var mn = ('0' + (date.getMonth() + 1)).slice(-2);
var dt = ('0' + date.getDate()).slice(-2);
var yr = date.getFullYear();
var date = yr+'-'+mn+'-'+dt;

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

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

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

var dateTime = date+' '+time;

 

You might also like:

How to convert html to pdf in Node js

12-03-2017 pdf convert html node js

Filter for change Date format (MM-dd-yyyy) in Angular Js

05-03-2017 mm-dd-yyyy date formats angular js

Filter for replace text in Angular Js

05-03-2017 filter replace angular js

How to get drop down value when drop down change using javascript

27-02-2017 onchange events dropdown javascript functions

Email validation regular expression in javascript

22-02-2017 email validation regex