WEB基础之:JavaScript Date时间处理

1. 语法

new Date();
new Date(value);
new Date(dateString);
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);

1.1 参数

  • 空:如果没有提供参数,那么新创建的Date对象表示实例化时刻的日期和时间。

  • value:一个 Unix 时间戳(Unix Time Stamp),它是一个整数值,表示自1970年1月1日00:00:00 UTC(the Unix epoch)以来的毫秒数,忽略了闰秒。请注意大多数 Unix 时间戳功能仅精确到最接近的秒。

  • dateString:表示日期的字符串值。该字符串应该能被 Date.parse() 正确方法识别。

  • 分别提供日期与时间的每一个成员
    当至少提供了年份与月份时,这一形式的 Date() 返回的 Date 对象中的每一个成员都来自下列参数。没有提供的成员将使用最小可能值(对日期为1,其他为0)。
    
    year:表示年份的整数值。 099会被映射至1900年至1999年,其它值代表实际年份。
    monthIndex:表示月份的整数值,从 01月)到 1112月)。
    
    可选项:
    date:表示一个月中的第几天的整数值,从1开始。默认值为1hours:表示一天中的小时数的整数值 (24小时制)。默认值为0(午夜)。
    minutes:表示一个完整时间(如 01:10:00)中的分钟部分的整数值。默认值为0。
    seconds:表示一个完整时间(如 01:10:00)中的秒部分的整数值。默认值为0。
    milliseconds:表示一个完整时间的毫秒部分的整数值。默认值为0

2. 方法

Date.now()

  • 返回自 1970-1-1 00:00:00 UTC至今所经过的毫秒数。

Date.parse()

  • 解析一个表示日期的字符串,并返回从 1970-1-1 00:00:00 所经过的毫秒数。(注意: 由于浏览器差异和不一致,强烈建议不要使用Date.parse解析字符串。)

Date.UTC()

  • 接受和构造函数最长形式的参数相同的参数(从2到7),并返回从 1970-01-01 00:00:00 UTC 开始所经过的毫秒数。

2.1 Getter方法

Getter方法Setter方法描述
Date.prototype.getDate()Date.prototype.setDate()根据本地时间返回指定日期对象的月份中的第几天(1-31)。
Date.prototype.getDay()根据本地时间返回指定日期对象的星期中的第几天(0-6)。
Date.prototype.getFullYear()Date.prototype.setFullYear()根据本地时间返回指定日期对象的年份(四位数年份时返回四位数字)。
Date.prototype.getHours()Date.prototype.setHours()根据本地时间返回指定日期对象的小时(0-23)。
Date.prototype.getMilliseconds()Date.prototype.setMilliseconds()根据本地时间返回指定日期对象的毫秒(0-999)。
Date.prototype.getMinutes()Date.prototype.setMinutes()根据本地时间返回指定日期对象的分钟(0-59)。
Date.prototype.getMonth()Date.prototype.setMonth()根据本地时间返回指定日期对象的月份(0-11)。
Date.prototype.getSeconds()Date.prototype.setSeconds()根据本地时间返回指定日期对象的秒数(0-59)。
Date.prototype.getTime()Date.prototype.setTime()返回从1970-1-1 00:00:00 UTC到该日期经过的毫秒数,对于1970-1-1 00:00:00 UTC之前的时间返回负值。
Date.prototype.getTimezoneOffset()返回当前时区的时区偏移。
Date.prototype.getUTCDate()Date.prototype.setUTCDate()根据世界时返回特定日期对象一个月的第几天(1-31).
Date.prototype.getUTCDay()根据世界时返回特定日期对象一个星期的第几天(0-6).
Date.prototype.getUTCFullYear()Date.prototype.setUTCFullYear()根据世界时返回特定日期对象所在的年份(4位数).
Date.prototype.getUTCHours()Date.prototype.setUTCHours()根据世界时返回特定日期对象当前的小时(0-23).
Date.prototype.getUTCMilliseconds()Date.prototype.setUTCMilliseconds()根据世界时返回特定日期对象的毫秒数(0-999).
Date.prototype.getUTCMinutes()Date.prototype.setUTCMinutes()根据世界时返回特定日期对象的分钟数(0-59).
Date.prototype.getUTCMonth()Date.prototype.setUTCMonth()根据世界时返回特定日期对象的月份(0-11).
Date.prototype.getUTCSeconds()Date.prototype.setUTCSeconds()根据世界时返回特定日期对象的秒数(0-59).
Date.prototype.getYear()Date.prototype.setYear()根据特定日期返回年份 (通常 2-3 位数). 使用 getFullYear() .

2.2 实例

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);

// 
// 使用 Date 对象
var start = Date.now();

// 调用一个消耗一定时间的方法:
doSomethingForALongTime();
var end = Date.now();
var e
lapsed = end - start; // 以毫秒计的运行时长



2.3 计算经过时间

// 使用 Date 对象
var start = Date.now();

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

// 调用一个消耗一定时间的方法:
doSomethingForALongTime();
var end = new Date();
var elapsed = end.getTime() - start.getTime(); // 运行时间的毫秒值
// 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);

// 获取自 Unix 起始时间以来经过的秒数
var seconds = Math.floor(Date.now() / 1000);
posted @ 2021-02-26 07:00  f_carey  阅读(9)  评论(0编辑  收藏  举报  来源