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

09-03-2017 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 redirect one page to another page in php
20-03-2017 redirect php
How to use http-post request in node js
14-03-2017 http-post module node js
Using slice remove last two characters from given string
07-03-2017 slice remove
Remove last two characters from given string
06-03-2017 substring javascript remove
Get previous date from given date in javascript
06-03-2017 date javascript previous date