JS Date object in

1, the constructor

Date object may be generated by the constructor, Date constructor can be placed in four different parameters

1.1, new Date (), returns the local time and date at this time date object

D = the let new new a Date (); 
the console.log (D);     // output Wed Jul 24 2019 10:26:37 GMT + 0800 ( China Standard Time)

 

1.2, new Date (milliseconds), by returning a number of milliseconds transition date object

Inside milliseconds parameter is an integer indicating the number of milliseconds between the date object from '1970/01/01 00:00:00' to the returned.

Note: The starting point for every minute of plus time zone is currently located, such as the time zone of Beijing for the East Area 8, the starting point is the actual time: '1970/01/01 08:00:00', that is Thu Jan 01 1970 08: 00:00 GMT + 0800 (China standard time)

var D1 = new new a Date (1000 * 60 *. 1); // Returns forward point compared to the standard time of 1 minute date object 
the console.log (D1); // output Thu Jan 01 1970 08:01:00 GMT + 0800 (China standard time) because it is in China, so is the eight districts in the east as a standard point in time 
d1 = new new a date (3600 -1000 * 24-*); // returns compared to standard time points backwards one day of date objects 
console .log (d1); // output Wed Dec 31 1969 08:00:00 GMT + 0800 ( China standard time)

 

1.3, new Date (format string), a date object returns through the string into

Format format string There are two main

  1.  "Yyyy / MM / dd HH: mm: ss" (recommended): If the time is omitted, the time Date object to 00:00:00.

  2.  "yyyy-MM-dd HH: mm: ss": If the time is omitted, the time Date object is 08:00:00 (plus the local time zone). If the time is omitted, the string error in IE

 

var dt = new new a Date ( '2014/12/25' ); 
the console.log (dt); // Thu On Dec 25, 2014 00:00:00 GMT + 0800 (China Standard Time) 
dt = new new a Date ( '2014/12 / 25 12:00:00 ' ); 
the console.log (dt); // Thu On Dec 25, 2014 12:00:00 GMT + 0800 (China standard time) 
 
dt = new new a Date (' 2014-12-25 ' ); 
the console.log (dt); // Thu On Dec 25, 2014 08:00:00 GMT + 0800 (China standard time) 
dt = new new a Date ( '2014-12-25 12:00:00'); //   this IE will be given in the embodiment is not omitted when the time! 
console.log (dt); //   Thu Dec 25 2014 12:00:00 GMT + 0800 (China Standard Time)

 

 

1.4, new Date (year, month, day, hours, minutes, seconds, milliseconds), the date, hour, minute into date objects

parameter:

1) year: integer, 4 or 2 digits. Such as: Four 1999 is the year 1999, two in 1979 represented 97

2) month: integer, 2 digits. Counted from 0, 0 for January, 11 is December.

3) opt_day: integer, alternatively, two figures

4) opt_hours: integer, alternatively, two numbers, the range of 0 to 23.

5) opt_minutes: integer, alternatively, two numbers, the range of 0 to 59.

6) opt_seconds: integer, alternatively, two numbers, the range of 0 to 59.

7) opt_milliseconds: integer, optionally, from 0 to 999.

var dt = new new a Date (2014, 11);     // 2014 Nian 12 Month (the month number is entered here 11) 
console.log (dt); // Mon Dec 01 2014 00:00:00 GMT + 0800 (Chinese standard time) 

dt = new new a Date (2014, 11, 25); // 2014 Nian 12 Yue 25 Ri 
console.log (dt); // Thu Dec 25 2014 00:00:00 GMT + 0800 (China standard time) 

dt = new new a Date (2014, 12, 25); // 2014 on 13 January 25 (January 12 digital input here, represents the 13th month, jumping to second-year in January) 
console.log (dt); // the Sun Jan 25, 2015 00:00:00 GMT + 0800 (China standard time) 

dt = new new a Date (, 2014,. 11, 25, 15, 30, 40); //At 15:30:40 on December 25, 2014 
console.log (dt); // Thu Dec 25 2014 15:30:40 GMT + 0800 (China Standard Time)

 

2, an example method

2.1, get method

  1. getFullYear (): Year Date object value; 4 years.

  2. getMonth (): Returns the month value of a Date object. Starting from 0, so the real return value + 1 month =.

  3. getDate (): Date Date object of the month; values ​​range from 1 to 31.

  4.  getHours (): The hour Date object.

  5. () GetMinutes: Returns the value of the minutes of the Date object.

  6. getSeconds (): Returns the seconds value of the Date object.

  7. getMilliseconds (): Date object of milliseconds.

  8. getDay (): Week Date object of the week; 0 is Sunday, 1 is Monday, 2 is Tuesday, and so on

  9. the getTime (): returns the Date object '1970/01/01 00:00:00' millisecond value (GMT time zone between the East region 8, the starting point is the actual time: '1970/01/01 08:00: 00 ').

2.2, set method

  1. setFullYear (year, opt_month, opt_date): Date object year value; 4 years.

  2. setMonth (month, opt_date): Sets the month value in the Date object. 0 for January, 11 is December.

  3. setDate (date): Date Value Date object of the month; values ​​range from 1 to 31.

  4. setHours (hour, opt_min, opt_sec, opt_msec): The hour Date object.

  5. setMinutes (min, opt_sec, opt_msec): setting the minutes value in the Date object.

  6. setSeconds (sec, opt_msec): setting the second value of the Date object.

  7. setMilliseconds (msec): Sets the milliseconds of the Date object.

2.3 Other methods

  1. toString (): to convert a Date 'year, month, day, hour' string

  2. toLocaleString (): to convert a Date 'year, month, day, hour' native format string

  3. toDateString (): Date to convert a 'date' string

  4.  toLocaleDateString (): Date to convert a 'date' native format string

  5.  toTimeString (): 'hour, minute' into a string to Date

  6.  toLocaleTimeString (): to convert a Date 'minutes and seconds' native format string

  7. valueOf (): the getTime () as the Date object '1970/01/01 00:00:00' millisecond value between (GMT time zone is Eastern region 8, the starting point is the actual time: '1970/01 / 01 08:00:00 ') 

 

Reference: https://www.cnblogs.com/polk6/p/4156595.html#Menu5-Eg

 

 

Guess you like

Origin www.cnblogs.com/wenxuehai/p/11236708.html