JavaScript Date和时间

x33g5p2x  于2022-05-19 转载在 Java  
字(4.1k)|赞(0)|评价(0)|浏览(213)

在本教程中,您将借助示例了解 JavaScript 中的 Date 和时间。
    在 JavaScript 中,Date 和时间由 Date 对象表示。Date 对象提供日期和时间信息,还提供各种方法。
    JavaScript Date 定义了 EcmaScript 纪元,表示自 1970 年 1 月 1 日 UTC以来的毫秒数。此日期和时间与 UNIX 纪元(计算机记录的日期和时间值的主要基准值)相同。

创建 Date 对象

有四种方法可以创建日期对象。

  • new Date()
  • new Date(milliseconds)
  • new Date(Date string)
  • new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date()

您可以使用 new Date() 构造函数创建日期对象。例如,

const timeNow = new Date();
console.log(timeNow); // shows current date and time

输出

Mon Jul 06 2020 12:03:49 GMT+0545 (Nepal Time)

在这里,new Date() 使用当前日期和本地时间创建一个新的日期对象。

new Date(milliseconds)

Date 对象包含一个数字,表示自1970 年 1 月 1 日 UTC 以来的毫秒数。
    new Date(milliseconds) 通过将毫秒添加到零时间来创建一个新的日期对象。例如,

const time1 = new Date(0);

// epoch time
console.log(time1); // Thu Jan 01 1970 05:30:00

// 100000000000 milliseconds after the epoch time
const time2 = new Date(100000000000)
console.log(time2); // Sat Mar 03 1973 15:16:40

注意:1000 毫秒等于 1 秒。

new Date(date string)

new Date(date string) 从日期字符串创建一个新的日期对象。
    在 JavaScript 中,一般有三种日期输入格式。

ISO 日期格式

您可以通过传递 ISO 日期格式来创建日期对象。例如,

// ISO Date(International Standard)
const date = new Date("2020-07-01");

// the result date will be according to UTC
console.log(date); // Wed Jul 01 2020 05:45:00 GMT+0545

您也可以只传递年份和月份或仅传递年份。例如,

const date = new Date("2020-07");
console.log(date); // Wed Jul 01 2020 05:45:00 GMT+0545

const date1 = new Date("2020");
console.log(date1); // Wed Jul 01 2020 05:45:00 GMT+0545

您还可以将特定时间传递给 ISO 日期。

const date = new Date("2020-07-01T12:00:00Z");
console.log(date); // Wed Jul 01 2020 17:45:00 GMT+0545

注意:日期和时间用大写字母 T 分隔。UTC 时间用大写 Z 定义。

短日期和长日期格式

另外两种日期格式是短日期格式和长日期格式。

// short date format "MM/DD/YYYY"
const date = new Date("03/25/2015");
console.log(date); // Wed Mar 25 2015 00:00:00 GMT+0545

// long date format "MMM DD YYYY"
const date1 = new Date("Jul 1 2020");
console.log(date1); // Wed Jul 01 2020 00:00:00 GMT+0545

// month and day can be in any order
const date2 = new Date("1 Jul 2020");
console.log(date2); // Wed Jul 01 2020 00:00:00 GMT+0545

// month can be full or abbreviated. Also month names are insensitive.
// comma are ignored
const date3 = new Date("July 1 2020");
console.log(date3); // Wed Jul 01 2020 00:00:00 GMT+0545

const date4 = new Date("JULY, 1, 2020");
console.log(date4); // Wed Jul 01 2020 00:00:00
new Date(year, month, day, hours, minutes, seconds, milliseconds)

new Date(year, month,…) 通过传递特定的日期和时间创建一个新的日期对象。例如,

const time1 = new Date(2020, 1, 20, 4, 12, 11, 0);
console.log(time1); // Thu Feb 20 2020 04:12:11

传递的参数具有特定的顺序。
    如果传递四个数字,则表示年、月、日和小时。例如,

const time1 = new Date(2020, 1, 20, 4);
console.log(time1); // Thu Feb 20 2020 04:00:00

同样,如果传递了两个参数,则表示年份和月份。例如,

const time1 = new Date(2020, 1);
console.log(time1); // Sat Feb 01 2020 00:00:00

注意:如果您只传递一个参数,则将其视为毫秒。因此,您必须传递两个参数才能使用此日期格式。
    在 JavaScript 中,月份从 0 到 11 计数。一月是 0,十二月是 11。

JavaScript Date 方法

JavaScript Date 对象中有多种方法可用。

方法描述
now()返回当前时间对应的数值(自 1970 年 1 月 1 日 00:00:00 UTC 以来经过的毫秒数)
getFullYear()根据本地时间获取年份
getMonth()获取月份,根据本地时间从 0 到 11
getDate()根据本地时间获取月份中的第几天(1-31)
getDay()根据本地时间获取星期几(0-6)
getHours()根据当地时间获取从0到23的小时数
getMinutes根据当地时间获取从0到59的分钟数
getUTCDate()根据通用时间获取月份中的第几天 (1–31)
setFullYear()根据本地时间设置全年
setMonth()根据本地时间设置月份
setDate()根据本地时间设置月份中的某一天
setUTCDate()根据通用时间设置月份中的日期
示例:Date 方法
const timeInMilliseconds = Date.now();
console.log(timeInMilliseconds); // 1593765214488

const time = new Date;

// get day of the month
const date = time.getDate();
console.log(date); // 30

// get day of the week
const year = time.getFullYear();
console.log(year); // 2020

const utcDate = time.getUTCDate();
console.log(utcDate); // 30

const event = new Date('Feb 19, 2020 23:15:30');
// set the date
event.setDate(15);
console.log(event.getDate()); // 15

// Only 28 days in February!
event.setDate(35);

console.log(event.getDate()); // 7
格式化日期

与其他编程语言不同,JavaScript 不提供用于格式化日期的内置函数。
    但是,您可以提取单个位并像这样使用它。

const currentDate = new Date();
const date = currentDate.getDate();
const month = currentDate.getMonth();
const year = currentDate.getFullYear();

// show in specific format
let monthDateYear = (month+1) + '/' + date + '/' + year;

console.log(monthDateYear); // 7/3/2020

注意:上述程序给出的长度不一致,因为日期和月份有一位数和两位数。

Date 对象中的自动更正

在 Date 对象中指定超出范围的值时,它会自动更正自身。例如,

const date = new Date(2008, 0, 33);
// Jan does not have 33 days

console.log(date);

输出

Sat Feb 02 2008

要了解有关 JavaScript 中日期和时间的更多信息,请访问 Demystifying Date and Time

上一教程 :JS JSON                                          下一教程 :JS Closure

参考文档

[1] Parewa Labs Pvt. Ltd. (2022, January 1). Getting Started With JavaScript, from Parewa Labs Pvt. Ltd: https://www.programiz.com/javascript/date-time

相关文章