JavaScript Date object

JavaScript Date Object

The Date object is used to handle dates and times. The syntax for creating a Data object is as follows:

var date_obj = new Date( arg )

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

arg Parameter Description:

Parameter FormatParameter Description and Example

milliseconds Numeric format, indicating the number of milliseconds from 0:00 on January 1, 1970 to the number new Date(1289403980906)

datestring Date and time represented by a string. If time is omitted, it defaults to 0 o'clock. new Date("Mar 04, 2012 22:15:14")

year, month 4-digit year, 0-11 represents January to December respectively new Date(2012, 3)

year, month, day day Use 1-31 to represent a day in the month new Date(2012, 3, 4)

year, month, day, hours hours Use 0-23 to represent 24 hours in a day new Date(2012, 3, 4, 22)

year, month, day, hours, minutes minutes Use 0 -59 represents the number of minutes new Date( 2012, 3, 4, 22, 15 )

year, month, day, hours, minutes, seconds seconds Use 0-59 to represent the number of seconds new Date( 2012, 3, 4, 22, 15, 14 )

year, month, day, hours, minutes, seconds, microseconds microseconds uses 0-999 to represent the number of milliseconds new Date( 2012, 3, 4, 22, 15, 14, 100)

Creation Date

Date object is used to handle date and time.

Date objects can be defined through the new keyword. The following code defines a Date object named myDate:

There are four ways to initialize the date:

new Date() // Current date and time
new Date(milliseconds ) //Return the number of milliseconds since January 1, 1970
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

Most of the above parameters are optional. If not specified, the default parameter is 0.

Some examples of instantiating a date:

var today = new Date()
var d1 = new Date("October 13, 1975 11:13:00")
var d2 = new Date(79,5,24)
var d3 = new Date(79,5,24,11,33,0)

Set the date

We can easily set the date by using the methods on the date object. operate.

In the following example, we set a specific date (January 14, 2010) for the date object:

var myDate=new Date();
myDate.setFullYear(2010,0,14);

In the following example, we set the date object to a date 5 days later:

var myDate= new Date();
myDate.setDate(myDate.getDate()+5);

Note: If adding days will change the month or year, then the date object will automatically complete this conversion .

    php中文网(php.cn)  

Comparison of two dates

Date objects can also be used to compare two dates.

The following code compares the current date with January 14, 2100:

var x=new Date();
x.setFullYear(2100,0 ,14);
var today = new Date();

if (x>today)
{
alert("Today is before January 14, 2100");
}
else
{
alert("Today is after January 14, 2100");
}


Continuing Learning
||
php中文网(php.cn)
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!