Friday, June 15, 2018


How to get current Date and Time using JavaScript || JavaScript Date Formats

JavaScript Date Format || PhpMyPassion

This tutorial shows, how can you get the current date and time in JavaScript. Use the following JavaScript code to get current date and time with Y-m-dand H:i:s format.

JavaScript Date object help us to work with date type of strings. Use new Date() to create a new object with current date and time.
var today = new Date();
You can get current date from Date object in Y-m-d format.
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
You can get current time from Date object in H:i:s format.
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
You can  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;

JavaScript Date Format :-

There are 4 types of JavaScript date input formats.
  1. ISO Dates
  2. Long Dates
  3. Short Dates
  4. Full Format

JavaScript ISO Dates -

ISO 8601 is the international standard for the representation of dates and times.

The ISO 8601 syntax (YYYY-MM-DD) is more preferred JavaScript date format. 

Syntax as below: 

var date_o = new Date("2015-03-25");

JavaScript Short Dates -

Here short dates are written differently with an formate "MM/DD/YYYY" syntax as below:

var date_o = new Date("03/25/2015");

JavaScript Long Dates-

Here long dates are most often written with a "MMM DD YYYY" syntax as below:

var date_o = new Date("Mar 25 2015");

Full Date Format -

JavaScript accept date strings in the format of "full JavaScript format": syntax as like this ;:

var date_o = new Date("Fri Mar 25 2015 09:56:24 GMT+0100 (Tokyo Time)");

About Author -

Hi, I am Anil.

Welcome to my eponymous blog! I am passionate about web programming. Here you will find a huge information on web development, web design, PHP, Python, Digital Marketing and Latest technology.

Subscribe to this Blog via Email :