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:

File Upload Using Angular Js and Node Js

28-05-2018 AngularJs Node js file upload multer module

Detect AdBlock with JavaScript in my browser

30-07-2017 AdBlock javascript detect

How To Get Your Blogs Posts Using Blogger API

30-07-2017 blogger api json php

How to make an http request in node js

14-03-2017 http module node js

How to set or update or push the values into Object in javacript

27-02-2017 javascript set update push object