迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > TypeScript >

在 TypeScript 中格式化日期和时间

作者:迹忆客 最近更新:2023/03/19 浏览次数:

本篇文章将介绍内置对象 Date() 并讨论在 Typescript 中获取、设置和格式化日期和时间的各种方法。

TypeScript 中没有参数的日期对象

每当在没有任何参数的情况下调用构造函数 Date() 时,它都会返回编译器正在运行的当前设备的日期和时间。

例子:

let myDate: Date = new Date();
console.log('My date and time is = ' + myDate);

输出:

My date and time is = Fri Feb 18 2022 19:45:32 GMT+0000 (Country Standard Time)

TypeScript 中带有一个参数的日期对象

例子:

let myDate: Date = new Date(600000000000);
console.log('My date and time is = ' + myDate);

输出:

My date and time is = Thu Jan 05 1989 15:40:00 GMT+0000 (Country Standard Time)

在上面的例子中,格式与上一个相同,但时间和日期不同。Date() 构造函数现在有一个参数毫秒 添加到编译器开始时间和日期。

一般来说,编译器的开始日期和时间是 Thu Jan 01 1970 00:00:00。那么 600000000000 毫秒等于 166666.667 小时。

因此,将这些小时添加到编译器开始日期和时间会给出输出 Thu Jan 05 1989 15:40:00

TypeScript 中带有字符串参数的日期对象

示例 1:

let myDate: Date = new Date("2018-02-08T10:30:35");
console.log('My date and time is = ' + myDate);

输出:

My date and time is = Thu Feb 08 2018 10:30:35 GMT+0000 (Country Standard Time)

在输出中,显示用户设置的日期和时间。注意 Date() 构造函数中传递的参数是一个字符串。

字符串参数的语法:

(year-month-date T hours: minutes: seconds)

在这里,T 将日期与时间分开。

如果用户只提及年份,编译器将采用默认日期和月份。

示例 2:

let myDate: Date = new Date("2018T10:30:35");
console.log('My date and time is = ' + myDate);

输出:

My date and time is = Thu Jan 01 2018 10:30:35 GMT+0000 (Country Standard Time)

TypeScript 中具有多个参数的日期对象

示例 1:

let myDate: Date = new Date(2018, 9, 2, 12, 15, 45, 2000);
console.log('My date and time is = ' + myDate);

输出:

My date and time is = Tue Oct 02 2018 12:15:47 GMT+0000 (Country Standard Time)

日期和时间的格式仍然与上面的示例相同,但现在,Date() 构造函数有多个参数。

多个参数的语法:

(year, month, date, hours, minutes, seconds, milliseconds)

在上面的示例中,请注意 seconds 的值是 45,而 milliseconds 的值是 2000。这就是为什么计算的时间是 12 : 15 : 47

如果用户只输入四个参数,这些值将被依次赋值,其余的将被赋予默认值。

示例 2:

let myDate: Date = new Date(2018, 9, 2, 12);
console.log('My date and time is = ' + myDate);

输出:

My date and time is = Tue Oct 02 2018 12:00:00 GMT+0000 (Country Standard Time)

在 TypeScript 中使用 get 方法获取日期和时间

如上所示,Date() 对象为我们提供了有关日期和时间的所有详细信息。如果用户只想从整个字符串中获取一个值,则用户必须使用内置的 get 方法来获取相应的值。

例子:

let myDate: Date = new Date(2018, 9, 2, 12, 15, 45);
console.log('My date and time is = ' + myDate);

console.log('The year is = ' + myDate.getFullYear());
console.log('The month is = ' + myDate.getMonth());
console.log('The date is = ' + myDate.getDate());
console.log('The hours are = ' + myDate.getHours());
console.log('The minutes are = ' + myDate.getMinutes());
console.log('The seconds are = ' + myDate.getSeconds());

输出:

My date and time is = Tue Oct 02 2018 12:15:45 GMT+0000 (Country Standard Time)
The year is = 2018
The month is = 9
The date is = 2
The hours are = 12
The minutes are = 15
The seconds are = 45

在上面的代码中,使用了许多内置的 get 方法。正如名称描述它们的功能一样,每个方法都会相应地返回值。

同样,如果缺少任何值,将在输出中打印默认值。获取日期和时间还有许多其他内置方法。

在 TypeScript 中使用 set 方法设置日期和时间

如果用户想要为特定实体设置新值,而不是再次实例化 Date 对象,set 方法可以提供帮助。

例子:

let myDate: Date = new Date(2018, 9, 2, 12, 15, 45);
console.log('My date and time is = ' + myDate);

myDate.setFullYear(2020);
myDate.setMonth(7);
myDate.setDate(19);
myDate.setHours(16);
myDate.setMinutes(33);
myDate.setSeconds(12);

console.log('My updated date and time is = ' + myDate);

输出:

My date and time is = Tue Oct 02 2018 12:15:45 GMT+0000 (Country Standard Time)
My updated date and time is = Wed Aug 19 2020 16:33:12 GMT+0000 (Country Standard Time)

请注意上例中先前和更新的日期和时间之间的差异。还有许多其他内置方法可以用来设置我们可以使用的日期和时间。

在 TypeScript 中格式化日期和时间的方法

我们有许多用于格式化日期和时间的内置方法,每个方法都执行特定的操作。以下是最常用的方法。

例子:

let myDate: Date = new Date(2018, 9, 2, 12, 15, 45);
console.log('My date and time is = ' + myDate);

console.log('The ISO 8601 formatted date and time is = ' + myDate.toISOString());
console.log('The Britian formatted date and time is = ' + myDate.toLocaleString("en-GB"));
console.log('The American formatted date and time is = ' + myDate.toLocaleString("en-US"));
console.log(JSON.stringify({
    myJSONDate: myDate
}));

输出:

My date and time is = Tue Oct 02 2018 12:15:45 GMT+0000 (Country Standard Time)
The ISO 8601 formatted date and time is = 2018-10-02T07:15:45.000Z
The Britian formatted date and time is = 02/10/2018, 12:15:45
The American formatted date and time is = 10/2/2018, 12:15:45 PM
{"myJSONDate":"2018-10-02T07:15:45.000Z"}

注意上面例子中不同的方法是如何格式化日期和时间的。用户希望以哪种格式显示日期和时间取决于用户。

在 API 引用期间使用 toJSON() 方法。

toLocaleString() 用于根据地区格式化日期和时间。

toISOString() 用于根据 ISO 8601 格式格式化日期和时间。

还有许多其他方法可以格式化日期和时间。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 TypeScript 中返回一个 Promise

发布时间:2023/03/19 浏览次数:182 分类:TypeScript

本教程讨论如何在 TypeScript 中返回正确的 Promise。这将提供 TypeScript 中 Returns Promise 的完整编码示例,并完整演示每个步骤。

检查 TypeScript 中的 undefined

发布时间:2023/03/19 浏览次数:144 分类:TypeScript

本教程说明了 Typescript 中未定义检查的解决方案。这将提供 Typescript 中未定义类型检查的完整编码示例,并完整演示每个步骤。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便