JavaScript Date (date) object

Source: Internet
Author: User

Date objects are used to process dates and times.

How to use the date () method to get the day.

getFullYear ()
Use getFullYear () to get the year.

GetTime ()
GetTime () returns the number of milliseconds since January 1, 1970.

setFullYear ()
How to use setFullYear () to set a specific date.

toUTCString ()
How to use toUTCString () to convert the date of the day (based on UTC) to a string.

GetDay ()
How to use GetDay () and an array to display the week, not just the numbers.

Display a clock
How to display a clock on a webpage.

Complete reference manual for Date objects

We provide a reference manual for the JavaScript Date object, which includes all the properties and methods available for date objects. JavaScript Date Object Reference Manual.

The manual contains a detailed description of each of the properties and methods and the relevant examples.

Date Created

The Date object is used to process dates and times.

You can define a Date object by using the New keyword. The following code defines a Date object named MyDate:

There are four ways to initialize a date:

New Date ()//current date and time
New Date (milliseconds)//returns 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, and in the case of unspecified, the default parameter is 0.

<p from 1970 = "year =" "1 =" "month =" "Day Universal One day calculated as 86,400,000 milliseconds <=" "p=" ">

Instantiate some examples of 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 Date

By using a method for a Date object, we can easily manipulate the date.

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

var mydate=new Date ();
Mydate.setfullyear (2010,0,14);

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

var mydate=new Date ();
Mydate.setdate (Mydate.getdate () +5);

Note: If you increase the number of days to change the month or year, the Date object will automatically complete the conversion.

Two comparison of dates

A Date object 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 14th January 2100");
}
Else
{
Alert ("Today is after 14th January 2100");
}

JavaScript Date (date) object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.