JavaScript's Date object

Date Object

  Date is a constructor, first by new Date () to create an instance of an object, provide examples of members.

   Create a Date instance to handle dates and times. Date object based on the number of milliseconds (UTC) from the January 1, 1970.

Construction method:

  1, empty constructor

var now = new Date();    

    By empty argument constructor, to get the current time, UTC world time, distance (UTC) since January 1, 1970 the number of milliseconds

  2, the number of milliseconds to acquire time

var date1 = new Date (149809900356); // Get the specified number of milliseconds by date

  3, the date format string

var date2 = new Date ( '2019-8-16'); // get the date specified by date

  4, year, month, day format

var date3 = new Date (2019,7,16); // by specifying the year / month / day date of acquisition

Method acquisition date specified in milliseconds:

  1, General procedure

a Date now new new = var (); 
the console.log (now.valueof ()); // valueOf for obtaining the object's original value 
console.log (now.getTime ());

  Method 2, HTML5 of

Date.now now = var ();                 // HTML5 provides static members, there are compatibility issues,

  3, other methods

// do not support HTML5 browsers, this embodiment can use the following 
var now = + new Date () ; // call the Date object corresponds valueOf () method 
var now = Number (new Date ( )) 

Date Format Method:

toString () // date into a string 
valueof () // Get milliseconds date 
toDateString () // date into a string 
toTimeString () // time into a string 
toLocaleDateString () // date into local date string 
toLocaleTimeString () // local time into a time string

  Note: After four methods may appear inconsistent in different browsers, generally do not.

Gets the specified date part:

getTime () // returns the number of milliseconds and valueof () the same results, valueof () call getTime () 
getMilliseconds ()        
getSeconds () // return 0-59 
getMinutes () // return 0-59 
getHours () // return 0-23 
getDay () // returns the day of the week, Sunday 0, 6 Saturday 
getDate () // returns the current day of the month 
getMonth () // returns the month, start counting from 0 
getFullYear () // returns 4 Year-bit

Case:

  1, the date format object, and returns yyyy-MM-dd HH: mm: ss form of

. 1   function the formatDate (date) {
 2        // determines whether the parameter date is the date the object 
. 3        // the instanceof the instanceof instance (object)    
. 4        IF (! (Date the instanceof a Date)) {
 . 5          console.error ( 'date than the date of the object' )
 6          return ;
 . 7        }
 . 8  
. 9        var year = the Date.getFullYear (),
 10            month The date.getMonth = () +. 1 ,
 . 11            Day = date.getDate (),
 12 is            hour = date.getHours (),
 13 is            minute = date.getMinutes(),
14           second = date.getSeconds();
15 
16       month = month < 10 ? '0' + month : month;
17       day = day < 10 ? '0' + day : day;
18       hour = hour < 10 ? '0' + hour : hour;
19       minute = minute < 10 ? '0' + minute : minute;
20       second = second < 10 ? '0' + second : second;
21 
22       return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
23     }

  2, calculate the time difference, the phase difference returns days / hours / minutes / seconds

 1 function getInterval(start, end) {
 2       // 两个日期对象,相差的毫秒数
 3       var interval = end - start;
 4       // 求 相差的天数/小时数/分钟数/秒数
 5       var day, hour, minute, second;
 6 
 7       // 两个日期对象,相差的秒数
 8       interval /= 1000;
 9 
10       day = Math.round(interval / 60 / 60 / 24);
11       hour = Math.round(interval / 60 / 60 % 24);
12       minute = Math.round(interval / 60 % 60);
13       second = Math.round(interval % 60);
14     // 返回一个对象,键值对
15       return {
16         day: day,
17         hour: hour,
18         minute: minute,
19         second: second
20       }
21     }

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11363101.html