Common methods of Date in javascript

First, the constructor of Date

There are four forms of Date constructor:

// 1. The constructor has no parameters, then returns the Date object of the current date 
var now= new Date();
 // 2. The parameter of the constructor is the number of milliseconds of the date, and returns the milliseconds since January 1, 1970 Corresponding date 
var date= new Date(1222233 );
 // 3. The parameter of the constructor is the corresponding date string and returns the corresponding date object, in which year, month, day are required, hours, minutes and seconds are optional 
// actual Above, the above directly passes the string representing the date to the Date constructor, which will call Date.parse in the background 
var date1= new Date('2016-01-01' ); 
 var date2= new Date('2016/01 /01 12:00:00' ); 
 // 4. The parameters of the constructor are the year, the month based on 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 number of days is assumed to be 1, and if other parameters are omitted, it is assumed to be 0 
//In fact, the constructor for the above case calls Date.UTC in the background 
var date2= new Date(2016,4,5,17,55,55);

2. Return the milliseconds corresponding to the date

1.Date.parse()

Date.parse() takes a date string and returns the milliseconds corresponding to the date.

2.Date.UTC()

The parameters of Date.UTC() are year, 0-based month (0-11), day of the month (1-31), 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 number of days is assumed to be 1, and if other parameters are omitted, it is assumed to be 0. Returns the number of milliseconds corresponding to this date.

3. If there is a date object date, get its corresponding milliseconds, mostly using date.getTime() or +date

3. Get the milliseconds corresponding to the current time

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

Method 1: var start=Date.now();

Method 2: var end=+new Date();

Method 3: var end=new Date().getTime() 

Fourth, the common Date method

var date= new Date('2016-01-10' );
 var time=date.getTime() // Return the number of milliseconds corresponding to the date object, the same as the result returned by valueOf 
date.setTime(1); // with Set the date in milliseconds, which often changes the entire date object 
var year=date.getFullYear(); // Get the four-digit year, such as 2016 instead of 16 
date.setFullYear(2012); // Set the year, passed in The parameter must be a four -digit number 
var month=date.getMonth(); // Return the month of the date object (0-11) 
date.setMonth(0); // Set the month, the parameter must be a number from 0-11 
var day =date.getDate(); // Return the number of days in the month of the date object (1-31) 
date.setDate(11); // Set the number of days in the month, the parameter must be a number between 1-31 
varweek=date.getDay(); // return the number of days in the week of the date object (0-6) 
var hours=date.getHours(); // return the number of hours in a day (0-23) of the date object, corresponding to There are setHours 
var minutes=date.getMinutes(); // Returns the number of minutes in the date (0 to 59), corresponding to setMinutes 
var seconds=date.getSeconds(); // Returns the number of seconds in the date (0- 59), corresponding to setSeconds

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324450368&siteId=291194637