JavaScript get current date time

Sharad Jaiswal
Written by Sharad Jaiswal

Posted On: Nov 06, 2018

 

How to get current Date Time in JavaScript: In Easy Way

In this tutorial, we are going to see How to get the current Date time in JavaScript. We are going to use Javascript library function Date(). We will take an example to see how to format javascript date to MySQL format Y-m-d and H:i:s format.

By default Javascript Date() function return a current date time obj in UTC format/timezone.

Creating current DateTime Object using new Keyword.

let today= new Date();

Getting the current date from date object in Y-m-d format.

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

Note: JavaScript date object Month index will start from 0 so to get real Date time we have to add +1 in it.

Getting current time from Date object in H:i:s format

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

Getting Complete DateTime in Y-m-d H:i:s format from date object

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

    Please Login or Register to leave a response.

    Related Articles

    JavaScript Tutorials

    JavaScript Vs Typescript

    Javascript Vs. Typescript: JavaScript and TypeScript are reputed and much debated over topics among web developers. Instead of discussing the pros and cons of these two languages. Here are few Differe..

    JavaScript Tutorials

    5 JavaScript Frameworks to learn in 2018

    JavaScript is the latest buzz in the web development industry that is extensively used by the developers to develop sophisticated web applications that will be having rich client-side interactions. Th..

    JavaScript Tutorials

    Comparison between JavaScript Top Frameworks react vs angular 2, vue js vs react, ember vs react

    Comparing top 3 Javascript Frameworks of 2018: Angular 5, React.js and Vue.js. JavaScript has a number of frameworks and libraries with a variety of functions and features, some specific and some with..

    Ask a Question