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-d
and 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.
- ISO Dates
- Long Dates
- Short Dates
- 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)");
Note: Only a member of this blog may post a comment.