Javascript date time

The Date object is used to manipulate the date and time. The syntax for creating a Data object is as follows:

var date_obj = new Date(arg)

arg is an optional parameter to the Data object constructor. When this parameter is omitted, the Data object automatically saves the current date and time as an initial value. You can also specify the arg parameter to set the Date object's date and time values. The following parameters are acceptable:

arg parameters :

Parameter Format Parameter description and example
milliseconds A number format that represents the number of milliseconds to the number at 0:00 on January 1, 1970
new Date(1289403980906)
datestring The string represents the date and time, omit the time default is 0:00
new Date("Mar 04, 2012 22:15:14")
year, month 4 digits of the year, 0-11, respectively, 1-12 months
new Date(2012, 3)
year, month, day day, Use 1-31 to indicate the day of the month
new Date(2012, 3, 4)
year, month, day, hours hours, Use 0-23 for 24 hours a day
new Date(2012, 3, 4, 22)
year, month, day, hours, minutes minutes, 0-59
new Date(2012, 3, 4, 22, 15)
year, month, day, hours, minutes, seconds seconds, 0-59
new Date(2012, 3, 4, 22, 15, 14)
year, month, day, hours, minutes, seconds, microseconds microseconds, 0-999
new Date(2012, 3, 4, 22, 15, 14, 100)

Date Time Data objects obtained from the client browser

Date object example

var d = new Date();
document.write("Now is : " + d);

Run the example and output:

Now is : Sun Mar 04 22:15:14 2012

But in lower versions of IE (less than 8), the output will contain time zone information:

Now is : Sun Mar 4 22:15:14 UTC+0800 2012

If you simply want to output Date object information and do not want to include time zone information, use the following:


The Date object is formatted to local time

Date object provides toLocaleString method can be convenient to date and time into local format:

var d = new Date();
document.write("Now is : " + d.toLocaleString());

Run the example and output:

Now is : 10/01/2017, 6:47:11 PM

Copyright 2014-2017 by All Rights Reserved.