JavaScript in the date of the relevant operation method Summary _ Basics

Source: Internet
Author: User

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 of initializing the date:

New Date ()//current date and time
new Date (milliseconds)//returns the number of milliseconds from January 1, 1970 to date new date (
datestring)
new Date (year, MO Nth, day, hours, minutes, seconds, milliseconds)

Most of the parameters above are optional, and in the case of unspecified, the default parameter is 0.
Some examples of instantiating a date:

var today = new Date ()
var d1 = new Date ("October, 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 date objects, we can easily manipulate dates.
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 the date after 5 days:

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

Note: If increasing the number of days changes the month or year, the date object automatically completes the conversion.
Two date comparisons
Date objects can also be used to compare two dates.
The following code compares the current date with the 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 was after 14th January 2100");
 }

How to get the date of the day using the date () method.
Source code:

<! DOCTYPE html>
 
 

Test results:

Sat Oct 2015 15:14:48 gmt+0800 (China Standard Time)

Related Article

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.