Use JavaScript standard built-in object Date

Many development languages ​​have built-in Date objects, and JavaScript is no exception. This article is excerpted from the JavaScript standard built-in object Date to
create a JavaScript Date instance, which presents a moment in time. Date objects are based on Unix Time Stamp, which is the number of milliseconds that have passed since January 1, 1970 (UTC).
JavaScript Demo: Date Constructor

const date1 = new Date('December 17, 1995 03:24:00');
// Sun Dec 17 1995 03:24:00 GMT...

const date2 = new Date('1995-12-17T03:24:00');
// Sun Dec 17 1995 03:24:00 GMT...

console.log(date1 === date2);
// expected output: false;

console.log(date1 - date2);
// expected output: 0

For specific description and use, please refer to JavaScript standard built-in object Date

Example: Several methods to create a date object

The following example shows the various methods used to create a date object.
Note: Due to browser differences and inconsistencies, it is strongly recommended not to use the Date constructor (and Date.parse, which are equivalent) to parse date strings.

var today = new Date();
var birthday = new Date('December 17, 1995 03:24:00');
var birthday = new Date('1995-12-17T03:24:00');
var birthday = new Date(1995, 11, 17);
var birthday = new Date(1995, 11, 17, 3, 24, 0);

Example: Map two-digit years to 1900-1999

In order to create and get the year between 0 and 99, the Date.prototype.setFullYear () and Date.prototype.getFullYear () methods should be used.

var date = new Date(98, 1); // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT)

// 已弃用的方法, 同样将 98 映射为 1998
date.setYear(98);           // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT)

date.setFullYear(98);       // Sat Feb 01 0098 00:00:00 GMT+0000 (BST)

Example: Calculate the elapsed time

The following example shows how to calculate the time difference between two date objects with millisecond precision:

Due to the different lengths of different days, months, and years (the different lengths of the days come from the switch of daylight saving time), using units greater than seconds, minutes, and hours to express elapsed time will encounter many problems, and detailed research is required before use.

  • 1. Use Date objects
// 使用 Date 对象
var start = Date.now();

// 调用一个消耗一定时间的方法:
doSomethingForALongTime();
var end = Date.now();
var elapsed = end - start; // 以毫秒计的运行时长
  • 2. Use the built-in creation method
// 使用内建的创建方法
var start = new Date();

// 调用一个消耗一定时间的方法:
doSomethingForALongTime();
var end = new Date();
var elapsed = end.getTime() - start.getTime(); // 运行时间的毫秒值
  • 3. Write a function and test the time it takes
// to test a function and get back its return
function printElapsedTime (fTest) {
    var nStartTime = Date.now(),
        vReturn = fTest(),
        nEndTime = Date.now();
    alert("Elapsed time: " + String(nEndTime - nStartTime) + " milliseconds"); 
    return vReturn;
}
yourFunctionReturn = printElapsedTime(yourFunction);

Note: In browsers that support the high-resolution time function of the Web Performance API, the elapsed time provided by Performance.now () is more reliable and accurate than Date.now ().

  • 4. Get the number of seconds that have passed since the Unix start time
var seconds = Math.floor(Date.now() / 1000);

Note that you need to return an integer (only the division is not an integer), and you need to return the actual number of seconds that have elapsed (so Math.floor () is used instead of Math.round ()).

Published 131 original articles · Like 38 · Visit 990,000+

Guess you like

Origin blog.csdn.net/ccf19881030/article/details/104633214