前端技术学习路线图-初阶-JavaScript-日期和时间

日期和时间

创建一个 JavaScript Date 实例,该实例呈现时间中的某个时刻。Date 对象则基于 Unix Time Stamp,即自 1970 年 1 月 1 日(UTC)起经过的毫秒数。

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

语法:

new Date()
new Date(value)
new Date(dateString)
new Date(dateObject)

new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)

Date()

参数

对于 Date() 构造函数,有五种基本的形式:

  1. 无参数:当没有提供参数时,新创建的 Date 对象代表当前的日期和时间。
  2. 时间或时间戳值:一个整数,代表自 UTC 1970 年 1 月 1 日 00:00:00 (ECMAScript 纪元,与 UNIX 纪元相同)以来的毫秒数,忽略闰秒。
  3. 日期字符串:一个代表日期的字符串值,其格式由 Date.parse() 方法所识别。
  4. 日期对象:一个现有的 Date 对象。这实际上是在现有的 Date 对象上复制了一个相同的日期和时间。这等同于 new Date(dateObject.valueOf()),除了不调用 valueOf() 方法。
  5. 日期和时间组件的单独值:
    • year:表示年的整数。从 099 的值映射了 19001999 年。其他值对应真实的年份。
    • monthIndex:表示月份的整数,从代表一月的 0 开始到代表十二月的 11 结束。
    • day(可选):表示一个月中第几天的整数。默认为 1
    • hours(可选):表示一天中的小时数的整数值,在 023 之间。默认值为 0
    • minutes(可选):表示时间的分钟段的整数值。默认为小时后的 0 分钟。
    • seconds(可选):表示时间的秒数段的整数值。默认为分钟后的 0 秒。
    • milliseconds(可选):表示时间的毫秒段的整数值。默认为 0 毫秒的秒数。

Date.prototype.getDate()

getDate() 返回一个 1 到 31 的整数值。

const birthday = new Date('August 19, 1975 23:15:30');
const date1 = birthday.getDate();

console.log(date1);
// Expected output: 19

Date.prototype.getDay()

getDay() 方法根据本地时间,返回一个具体日期中一周的第几天,0 表示星期天。

const birthday = new Date('August 19, 1975 23:15:30');
const day1 = birthday.getDay();
// Sunday - Saturday : 0 - 6

console.log(day1);
// Expected output: 2

Date.prototype.getFullYear()

getFullYear() 方法根据本地时间返回指定日期的年份。

const moonLanding = new Date('July 20, 69 00:20:18');

console.log(moonLanding.getFullYear());
// Expected output: 1969

Date.prototype.getHours()

getHours() 方法根据本地时间,返回一个指定的日期对象的小时。

const birthday = new Date('March 13, 08 04:20');

console.log(birthday.getHours());
// Expected output: 4

Date.prototype.getMilliseconds()

getMilliseconds() 方法根据本地时间,返回一个指定的日期对象的毫秒数。

const moonLanding = new Date('July 20, 69 00:20:18');
moonLanding.setMilliseconds(123);

console.log(moonLanding.getMilliseconds());
// Expected output: 123

Date.prototype.getMinutes()

getMinutes() 方法根据本地时间,返回一个指定的日期对象的分钟数。

const birthday = new Date('March 13, 08 04:20');

console.log(birthday.getMinutes());
// Expected output: 20

Date.prototype.getMonth()

getMonth() 方法根据本地时间,返回一个指定的日期对象的月份,为基于 0 的值(0 表示一年中的第一月)。

const moonLanding = new Date('July 20, 69 00:20:18');

console.log(moonLanding.getMonth()); // (January gives 0)
// Expected output: 6

Date.prototype.getSeconds()

getSeconds() 方法根据本地时间,返回一个指定的日期对象的秒数。

const moonLanding = new Date('July 20, 69 00:20:18');

console.log(moonLanding.getSeconds());
// Expected output: 18

Date.prototype.getTime()

getTime() 方法返回一个时间的格林威治时间数值。

你可以使用这个方法把一个日期时间赋值给另一个 Date 对象。这个方法的功能和 valueOf() 方法一样。

const moonLanding = new Date('July 20, 69 20:17:40 GMT+00:00');

// Milliseconds since Jan 1, 1970, 00:00:00.000 GMT
console.log(moonLanding.getTime());
// Expected output: -14182940000

Date.prototype.getTimezoneOffset()

getTimezoneOffset() 方法返回协调世界时(UTC)相对于当前时区的时间差值,单位为分钟。

const date1 = new Date('August 19, 1975 23:15:30 GMT+07:00');
const date2 = new Date('August 19, 1975 23:15:30 GMT-02:00');

console.log(date1.getTimezoneOffset());
// Expected output: your local timezone offset in minutes
// (e.g., -120). NOT the timezone offset of the date object.

console.log(date1.getTimezoneOffset() === date2.getTimezoneOffset());
// Expected output: true

Date.prototype.getUTCDate()

getUTCDate() 方法以世界时为标准,返回一个指定的日期对象为一个月中的第几天

const date1 = new Date('August 19, 1975 23:15:30 GMT+11:00');
const date2 = new Date('August 19, 1975 23:15:30 GMT-11:00');

console.log(date1.getUTCDate());
// Expected output: 19

console.log(date2.getUTCDate());
// Expected output: 20

Date.prototype.getUTCDay()

getUTCDay() 方法以世界时为标准,返回一个指定的日期对象为一星期中的第几天,其中 0 代表星期天。

const date1 = new Date('August 19, 1975 23:15:30 GMT+11:00');
const date2 = new Date('August 19, 1975 23:15:30 GMT-11:00');

// Tuesday
console.log(date1.getUTCDay());
// Expected output: 2

// Wednesday
console.log(date2.getUTCDay());
// Expected output: 3

Date.prototype.getUTCFullYear()

getUTCFullYear() 以世界时为标准,返回一个指定的日期对象的年份。

const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');
const date2 = new Date('December 31, 1975, 23:15:30 GMT-11:00');

console.log(date1.getUTCFullYear());
// Expected output: 1975

console.log(date2.getUTCFullYear());
// Expected output: 1976

Date.prototype.getUTCHours()

getUTCHours() 方法以世界时为标准,返回一个指定的日期对象的小时数。

const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');
const date2 = new Date('December 31, 1975, 23:15:30 GMT-11:00');

console.log(date1.getUTCHours());
// Expected output: 12

console.log(date2.getUTCHours());
// Expected output: 10

Date.prototype.getUTCMilliseconds()

getUTCMilliseconds() 方法以世界时为标准,返回一个指定的日期对象的毫秒数。

const exampleDate = new Date('2018-01-02T03:04:05.678Z'); // 2 January 2018, 03:04:05.678 (UTC)

console.log(exampleDate.getUTCMilliseconds());
// Expected output: 678

Date.prototype.getUTCMinutes()

getUTCMinutes() 方法以世界时为标准,返回一个指定的日期对象的分钟数。

const date1 = new Date('1 January 2000 03:15:30 GMT+07:00');
const date2 = new Date('1 January 2000 03:15:30 GMT+03:30');

console.log(date1.getUTCMinutes()); // 31 Dec 1999 20:15:30 GMT
// Expected output: 15

console.log(date2.getUTCMinutes()); // 31 Dec 1999 23:45:30 GMT
// Expected output: 45

Date.prototype.getUTCMonth()

getUTCMonth() 方法以世界时为标准,返回一个指定的日期对象的月份,它是从 0 开始计数的(0 代表一年的第一个月)。

const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');
const date2 = new Date('December 31, 1975, 23:15:30 GMT-11:00');

// December
console.log(date1.getUTCMonth());
// Expected output: 11

// January
console.log(date2.getUTCMonth());
// Expected output: 0

Date.prototype.getUTCSeconds()

getUTCSeconds() 方法以世界时为标准,返回一个指定的日期对象的秒数。

const moonLanding = new Date('July 20, 1969, 20:18:04 UTC');

console.log(moonLanding.getUTCSeconds());
// Expected output: 4

Date.now()

Date.now() 方法返回自 1970 年 1 月 1 日 00:00:00 (UTC) 到当前时间的毫秒数。

// This example takes 2 seconds to run
const start = Date.now();

console.log('starting timer...');
// Expected output: "starting timer..."

setTimeout(() => {
  const millis = Date.now() - start;

  console.log(`seconds elapsed = ${Math.floor(millis / 1000)}`);
  // Expected output: "seconds elapsed = 2"
}, 2000);

Date.parse()

Date.parse() 方法解析一个表示某个日期的字符串,并返回从 1970-1-1 00:00:00 UTC 到该日期对象(该日期对象的 UTC 时间)的毫秒数,如果该字符串无法识别,或者一些情况下,包含了不合法的日期数值(如:2015-02-31),则返回值为 NaN。

不推荐在 ES5 之前使用 Date.parse 方法,因为字符串的解析完全取决于实现。直到至今,不同宿主在如何解析日期字符串上仍存在许多差异,因此最好还是手动解析日期字符串(在需要适应不同格式时库能起到很大帮助)。

const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT');
const javaScriptRelease = Date.parse('04 Dec 1995 00:12:00 GMT');

console.log(unixTimeZero);
// Expected output: 0

console.log(javaScriptRelease);
// Expected output: 818035920000

Date.prototype.setDate()

setDate() 方法根据本地时间来指定一个日期对象的天数。

const event = new Date('August 19, 1975 23:15:30');

event.setDate(24);

console.log(event.getDate());
// Expected output: 24

event.setDate(32);
// Only 31 days in August!

console.log(event.getDate());
// Expected output: 1

Date.prototype.setFullYear()

setFullYear() 方法根据本地时间为一个日期对象设置年份。

const event = new Date('August 19, 1975 23:15:30');

event.setFullYear(1969);

console.log(event.getFullYear());
// Expected output: 1969

event.setFullYear(0);

console.log(event.getFullYear());
// Expected output: 0

Date.prototype.setHours()

setHours() 方法根据本地时间为一个日期对象设置小时数,返回从 1970-01-01 00:00:00 UTC 到更新后的 日期 对象实例所表示时间的毫秒数。

const event = new Date('August 19, 1975 23:15:30');
event.setHours(20);

console.log(event);
// Expected output: "Tue Aug 19 1975 20:15:30 GMT+0200 (CEST)"
// Note: your timezone may vary

event.setHours(20, 21, 22);

console.log(event);
// Expected output: "Tue Aug 19 1975 20:21:22 GMT+0200 (CEST)"
Date.prototype.setMilliseconds()

setMilliseconds() 方法会根据本地时间设置一个日期对象的豪秒数。

const event = new Date('August 19, 1975 23:15:30');

console.log(event.getMilliseconds());
// Expected output: 0

event.setMilliseconds(456);

console.log(event.getMilliseconds());
// Expected output: 456

Date.prototype.setMinutes()

setMinutes() 方法根据本地时间为一个日期对象设置分钟数。

const event = new Date('August 19, 1975 23:15:30');

event.setMinutes(45);

console.log(event.getMinutes());
// Expected output: 45

console.log(event);
// Expected output: "Tue Aug 19 1975 23:45:30 GMT+0200 (CEST)"
// Note: your timezone may vary

Date.prototype.setMonth()

setMonth() 方法根据本地时间为一个日期对象设置月份。

const event = new Date('August 19, 1975 23:15:30');

event.setMonth(3);

console.log(event.getMonth());
// Expected output: 3

console.log(event);
// Expected output: "Sat Apr 19 1975 23:15:30 GMT+0100 (CET)"
// Note: your timezone may vary

Date.prototype.setSeconds()

setSeconds() 方法根据本地时间设置一个日期对象的秒数。

const event = new Date('August 19, 1975 23:15:30');

event.setSeconds(42);

console.log(event.getSeconds());
// Expected output: 42

console.log(event);
// Expected output: "Sat Apr 19 1975 23:15:42 GMT+0100 (CET)"
// Note: your timezone may vary

Date.prototype.setTime()

setTime() 方法以一个表示从 1970-1-1 00:00:00 UTC 计时的毫秒数为来为 Date 对象设置时间。

const launchDate = new Date('July 1, 1999, 12:00:00');
const futureDate = new Date();
futureDate.setTime(launchDate.getTime());

console.log(futureDate);
// Expected output: "Thu Jul 01 1999 12:00:00 GMT+0200 (CEST)"

const fiveMinutesInMillis = 5 * 60 * 1000;
futureDate.setTime(futureDate.getTime() + fiveMinutesInMillis);

console.log(futureDate);
// Expected output: "Thu Jul 01 1999 12:05:00 GMT+0200 (CEST)"
// Note: your timezone may vary

Date.prototype.setUTCDate()

setUTCDate() 方法就是根据全球时间设置特定 date 对象的日期。

const event = new Date('August 19, 1975 23:15:30 GMT-3:00');

console.log(event.getUTCDate());
// Expected output: 20

event.setUTCDate(19);

console.log(event.getUTCDate());
// Expected output: 19

Date.prototype.setUTCFullYear()

setUTCFullYear() 方法根据世界标准时间为一个具体日期设置年份。

const event = new Date('December 31, 1975 23:15:30 GMT-3:00');

console.log(event.getUTCFullYear());
// Expected output: 1976

console.log(event.toUTCString());
// Expected output: "Thu, 01 Jan 1976 02:15:30 GMT"

event.setUTCFullYear(1975);

console.log(event.toUTCString());
// Expected output: "Wed, 01 Jan 1975 02:15:30 GMT"

Date.prototype.setUTCHours()

settutchours() 方法根据世界时设置指定日期的小时,并返回从UTC时间1970年1月1日00:00:00到更新后的date实例所表示的时间的毫秒数。

const event = new Date('August 19, 1975 23:15:30 GMT-3:00');

console.log(event.toUTCString());
// Expected output: "Wed, 20 Aug 1975 02:15:30 GMT"

console.log(event.getUTCHours());
// Expected output: 2

event.setUTCHours(23);

console.log(event.toUTCString());
// Expected output: "Wed, 20 Aug 1975 23:15:30 GMT"

Date.prototype.setUTCMilliseconds()

setUTCMilliseconds() 方法会根据世界时来设置指定时间的毫秒数。

const date1 = new Date('2018-01-24T12:38:29.069Z');

console.log(date1.getUTCMilliseconds());
// Expected output: 69

date1.setUTCMilliseconds(420);

console.log(date1.getUTCMilliseconds());
// Expected output: 420

Date.prototype.setUTCMinutes()

**setUTCMinutes()**方法会根据世界协调时(UTC)来设置指定日期的分钟数。

const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');

console.log(date1.getUTCMinutes());
// Expected output: 15

date1.setUTCMinutes(25);

console.log(date1.getUTCMinutes());
// Expected output: 25

Date.prototype.setUTCMonth()

setUTCMonth() 方法根据通用的时间来设置一个准确的月份。

const event = new Date('December 31, 1975 23:15:30 GMT-3:00');

console.log(event.toUTCString());
// Expected output: "Thu, 01 Jan 1976 02:15:30 GMT"

console.log(event.getUTCMonth());
// Expected output: 0

event.setUTCMonth(11);

console.log(event.toUTCString());
// Expected output: "Wed, 01 Dec 1976 02:15:30 GMT"

Date.prototype.setUTCSeconds()

此 setUTCSeconds() 方法为一个依据国际通用时间的特定日期设置秒数。

const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');

console.log(date1.getUTCSeconds());
// Expected output: 30

date1.setUTCSeconds(39);

console.log(date1.getUTCSeconds());
// Expected output: 39

Date.prototype.toDateString()

toDateString() 方法以美式英语和人类易读的形式返回一个日期对象日期部分的字符串。

const event = new Date(1993, 6, 28, 14, 39, 7);

console.log(event.toString());
// Expected output: "Wed Jul 28 1993 14:39:07 GMT+0200 (CEST)"
// Note: your timezone may vary

console.log(event.toDateString());
// Expected output: "Wed Jul 28 1993"

Date.prototype.toISOString()

toISOString() 方法返回一个 ISO(ISO 8601 Extended Format)格式的字符串: YYYY-MM-DDTHH:mm:ss.sssZ。时区总是 UTC(协调世界时),加一个后缀“Z”标识。

const event = new Date('05 October 2011 14:48 UTC');
console.log(event.toString());
// Expected output: "Wed Oct 05 2011 16:48:00 GMT+0200 (CEST)"
// Note: your timezone may vary

console.log(event.toISOString());
// Expected output: "2011-10-05T14:48:00.000Z"

Date.prototype.toJSON()

toJSON() 方法返回 Date 对象的字符串形式。

const event = new Date('August 19, 1975 23:15:30 UTC');

const jsonDate = event.toJSON();

console.log(jsonDate);
// Expected output: "1975-08-19T23:15:30.000Z"

console.log(new Date(jsonDate).toUTCString());
// Expected output: "Tue, 19 Aug 1975 23:15:30 GMT"

Date.prototype.toLocaleDateString()

toLocaleDateString() 方法返回指定日期对象日期部分的字符串,该字符串格式因不同语言而不同。在支持 Intl.DateTimeFormat API 的实现中,该方法仅是调用了 Intl.DateTimeFormat 方法。

const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };

console.log(event.toLocaleDateString('de-DE', options));
// Expected output (varies according to local timezone): Donnerstag, 20. Dezember 2012

console.log(event.toLocaleDateString('ar-EG', options));
// Expected output (varies according to local timezone): الخميس، ٢٠ ديسمبر، ٢٠١٢

console.log(event.toLocaleDateString(undefined, options));
// Expected output (varies according to local timezone and default locale): Thursday, December 20, 2012

Date.prototype.toLocaleString()

toLocaleString() 方法返回该日期对象的字符串,该字符串格式因不同语言而不同。在支持 Intl.DateTimeFormat API 的实现中,该方法仅是调用了 Intl.DateTimeFormat 方法。

const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

// British English uses day-month-year order and 24-hour time without AM/PM
console.log(event.toLocaleString('en-GB', { timeZone: 'UTC' }));
// Expected output: "20/12/2012, 03:00:00"

// Korean uses year-month-day order and 12-hour time with AM/PM
console.log(event.toLocaleString('ko-KR', { timeZone: 'UTC' }));
// Expected output: "2012. 12. 20. 오전 3:00:00"

Date.prototype.toLocaleTimeString()

toLocaleTimeString() 方法返回该日期对象时间部分的字符串,该字符串格式因语言而异。在支持 Intl.DateTimeFormat API 的实现中,该方法仅是调用了 Intl.DateTimeFormat 方法。

// Depending on timezone, your results will vary
const event = new Date('August 19, 1975 23:15:30 GMT+00:00');

console.log(event.toLocaleTimeString('en-US'));
// Expected output: "1:15:30 AM"

console.log(event.toLocaleTimeString('it-IT'));
// Expected output: "01:15:30"

console.log(event.toLocaleTimeString('ar-EG'));
// Expected output: "١٢:١٥:٣٠ ص"

Date.prototype.toString()

toString() 方法返回一个字符串,以本地的时区表示该 Date 对象。

const event = new Date('August 19, 1975 23:15:30');

console.log(event.toString());
// Expected output: "Tue Aug 19 1975 23:15:30 GMT+0200 (CEST)"
// Note: your timezone may vary

Date.prototype.toTimeString()

toTimeString() 方法以人类易读形式返回一个日期对象时间部分的字符串,该字符串以美式英语格式化。

const event = new Date('August 19, 1975 23:15:30');

console.log(event.toTimeString());
// Expected output: "23:15:30 GMT+0200 (CEST)"
// Note: your timezone may vary

Date.prototype.toUTCString()

toUTCString() 方法把一个日期转换为一个字符串,使用 UTC 时区。

const event = new Date('14 Jun 2017 00:00:00 PDT');

console.log(event.toUTCString());
// Expected output: "Wed, 14 Jun 2017 07:00:00 GMT"

Date.UTC()

Date.UTC() 方法接受的参数同 Date 构造函数接受最多参数时一样,但该前者会视它们为 UTC 时间,其返回从 1970 年 1 月 1 日 00:00:00 UTC 到指定时间的毫秒数。

const utcDate1 = new Date(Date.UTC(96, 1, 2, 3, 4, 5));
const utcDate2 = new Date(Date.UTC(0, 0, 0, 0, 0, 0));

console.log(utcDate1.toUTCString());
// Expected output: "Fri, 02 Feb 1996 03:04:05 GMT"

console.log(utcDate2.toUTCString());
// Expected output: "Sun, 31 Dec 1899 00:00:00 GMT"

Date.prototype.valueOf()

valueOf() 方法返回一个 Date 对象的原始值。

const date1 = new Date(Date.UTC(96, 1, 2, 3, 4, 5));

console.log(date1.valueOf());
// Expected output: 823230245000

const date2 = new Date('02 Feb 1996 03:04:05 GMT');

console.log(date2.valueOf());
// Expected output: 823230245000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值