Common methods of date in JavaScript

Source: Internet
Author: User
Tags days in month

First, the constructor of date

There are four types of date constructors:

//1. The constructor does not have parameters, it returns the Date object of the current dayvarnow=NewDate ();//2. The parameter of the constructor is the number of milliseconds of the date, and the date corresponding to January 1, 1970 after that millisecond is returnedvarDate=NewDate (1222233);//3. The constructor parameter is the corresponding date string, which returns the corresponding Date object, where the year, month, day are required, and seconds and minutes are optional//In fact, the above, which directly passes a string representing a date to the date constructor, is called in the background date.parsevardate1=NewDate (' 2016-01-01 ')); varDate2=NewDate (' 2016/01/01 12:00:00 ')); //4. The parameters for the constructor are year, based on the month of 0 (0-11), the Day of the month (1-31), the number of hours (0-23), minutes, seconds, and milliseconds. //of These parameters, only the first two parameters (year and month) are required. If the number of days in the month is not provided, the assumption is that the number of days is 1, and if other parameters are omitted, all are assumed to be 0//in fact, the constructor for the above scenario calls the DATE.UTC in the backgroundvarDate2=NewDate (2016,4,5,17,55,55);

Second, the return date corresponds to the number of milliseconds

1.date.parse ()

Date.parse () receives a date string that returns the number of milliseconds that the date corresponds to.

2.DATE.UTC ()

The parameter parameters for DATE.UTC () are year, based on the month of 0 (0-11), the Day of the month (1-31), the number of hours (0-23), minutes, seconds, and milliseconds. Of these parameters, only the first two parameters (year and month) are required. If the number of days in the month is not provided, the assumption is that the number of days is 1, and if other parameters are omitted, all are assumed to be 0. Returns the number of milliseconds that the date corresponds to.

3. If you have a Date object date, get its corresponding number of milliseconds, mostly using date.gettime () or +date

Three, gets the current time corresponding to the number of milliseconds

This is often used to monitor how long a piece of code is running.

Method One: Var start=date.now ();

Method Two: Var end=+new Date ();

Method Three: Var end=new Date (). GetTime ()

Iv. Common Date methods

varDate=NewDate (' 2016-01-10 '));varTime=date.gettime ()//returns the number of milliseconds corresponding to the Date object, the same as the result returned by valueofDate.settime (1);//sets the date in milliseconds, which often changes the entire date objectvarYear=date.getfullyear ();//get four-digit years, such as 2016 instead ofDate.setfullyear (2012);//set the year, the passed in parameter must be a four-digit numbervarMonth=date.getmonth ();//returns the month of the Date object (0-11)Date.setmonth (0);//set the month, the argument must be a number 0-11varDay=date.getdate ();//returns the number of days in the month of the Date object (1-31)Date.setdate (11);//set number of days in month, parameter must be a number between 1-31varWeek=date.getday ();//returns the number of days in the week of the Date object (0-6)varHours=date.gethours ();//returns the number of hours in the day of the Date object (0-23), corresponding to the sethoursvarMinutes=date.getminutes ();//returns the number of minutes in a date (0 to 59), corresponding to a setminutesvarSeconds=date.getseconds ();//returns the number of seconds in a date (0-59), corresponding to a setseconds

Common methods of date in JavaScript

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.