如何在 TypeScript 中将日期转换为字符串?
在 Web 或移动应用程序上显示日期和时间是很常见的。作为用户,您是否见过任何应用程序按原样显示日期对象?这永远不会发生,因为它会使用户体验变差。因此,我们需要将日期对象转换为字符串。
将日期对象转换为字符串后,我们可以根据需要对其进行格式化。日期格式为"yyyy-mm-dd"、"dd-mm-yyyy"等。此外,我们可以对时间字符串执行相同操作。我们可以根据需要显示小时、分钟、秒和毫秒。在本教程中,用户将学习如何在 TypeScript 中将日期对象转换为字符串。
使用 Date 类的 toLocalString() 方法
日期对象的 toLocaleString() 方法用于根据特定语言环境将日期对象转换为字符串。语言环境是指特定区域。
语法
用户可以按照以下语法使用 toLocaleString() 方法将日期对象转换为字符串。
let new_Date: Date = new Date(); new_Date.toLocaleString(Locale, options);
在上述语法中,我们将 toLocaleString() 方法与日期类的对象一起使用。
参数
toLocaleString() 方法采用以下两个参数。
Locale - 它代表特定区域。ToLocaleString() 根据特定语言环境将日期对象转换为字符串。
Options - 它是一个包含不同选项的对象。toLocaleString() 方法根据选项返回日期字符串。
返回值
- toLocaleString() 方法根据传递给参数的值以字符串格式返回日期。
示例
在下面的示例中,我们创建了 Date 类的对象。之后,我们使用不带任何参数的 toLocaleString() 方法将 new_date 对象转换为日期字符串。
接下来,我们获取相同的 new_date 对象,并根据美国特定的语言环境将其转换为字符串。最后,我们还将包含 year 属性的选项对象作为 toLocaleString() 方法的第二个参数传递。
// 创建一个新的日期对象 let new_Date: Date = new Date(); // 将日期转换为字符串 let result: string = new_Date.toLocaleString(); console.log("The date string according to current locale is: " + result);); // 将日期对象转换为美国特定的日期字符串 result = new_Date.toLocaleString("en-US"); console.log("The date string according to US specific locale is: " + result);
编译后,它将生成以下 JavaScript 代码 -
// 创建一个新的日期对象 var new_Date = new Date(); // 将日期转换为字符串 var result = new_Date.toLocaleString(); console.log("The date string according to current locale is: " + result);); // 将日期对象转换为美国特定的日期字符串 result = new_Date.toLocaleString("en-US"); console.log("The date string according to US specific locale is: " + result);
输出
上述代码将产生以下输出 -
The date string according to current locale is: 12/14/2022, 10:38:03 AM The date string according to US specific locale is: 12/14/2022, 10:38:03 AM
在输出中,用户可以看到,当我们将选项传递给 toLocaleString() 方法时,它会根据传递的选项返回字符串。
使用 toString() 或 toISOString() 方法
在 TypeScript 中,toString() 方法将其他数据类型或对象的变量转换为字符串格式。因此,我们可以使用它来将日期对象转换为字符串格式。
toISOString() 方法在 Date 类中定义。我们可以使用 toISOString() 方法将日期对象转换为 ISO 字符串格式,即 YYYY-MM-DDTHH:mm:ss.sssZ。
语法
用户可以按照以下语法将 toString() 和 toISOStirng() 方法与 Date 类的对象一起使用。
let date_String: string = new_Date().toString(); date_String = new_Date().toISOString();
在上述语法中,我们使用 toString() 和 toLocaleString() 方法将日期转换为字符串。
示例
在下面的示例中,current_date 是日期对象的实例。之后,我们使用 toString() 和 toISOString() 方法将日期对象转换为字符串。用户可以在输出中看到它按照 ISO 格式返回日期字符串。
let current_date: Date = new Date(); // 使用 toString() 方法 let date_String: string = current_date.toString(); console.log( "The date object after converting to string using the toString() method: " + date_String ); // 使用 toISOString() 方法 date_String = current_date.toISOString(); console.log( "The date object after converting to string using the toISOString() method: " + date_String );
编译后,它将生成以下 JavaScript 代码 -
var current_date = new Date(); // 使用 toString() 方法 var date_String = current_date.toString(); console.log("The date object after converting to string using the toString() method: " + date_String); // 使用 toISOString() 方法 date_String = current_date.toISOString(); console.log("The date object after converting to string using the toISOString() method: " + date_String);
输出
上述代码将产生以下输出 -
The date object after converting to string using the toString() method: Wed Dec 14 2022 10:45:11 GMT+0000 (UTC) The date object after converting to string using the toISOString() method: 2022-12-14T10:45:11.874Z
自定义函数将日期对象转换为字符串
创建自定义函数的方法是使用特定方法从日期对象中分别获取年、月、日、时、分和秒,并从中创建格式化字符串。在 TypeScript 中,我们可以使用 + 运算符或字符串文字 (${}) 来格式化字符串。
语法
用户可以按照以下语法创建自定义日期字符串。
let date_String: string = date_Object.getFullYear() + "/" + (date_Object.getMonth() + 1) + "/" + +date_Object.getDate() + " " + +date_Object.getHours() + ":" + +date_Object.getMinutes();
在上述语法中,我们展示了如何通过将年、月、日等分别与字符串联系起来,来创建自定义日期字符串。
示例
在下面的示例中,我们创建了名为 date_To_String 的函数,该函数创建并返回自定义日期字符串,如我们在上面的语法中所示。
function date_TO_String(date_Object: Date): string { // 分别获取年、月、日、小时和分钟并附加到字符串。 let date_String: string = date_Object.getFullYear() + "/" + (date_Object.getMonth() + 1) + "/" + +date_Object.getDate() + " " + +date_Object.getHours() + ":" + +date_Object.getMinutes(); return date_String; } let new_date: Date = new Date(); // 调用 date_TO_String 函数 let date_string = date_TO_String(new_date); console.log("The date string is " + date_string);
编译后,它将生成以下 JavaScript 代码 -
function date_TO_String(date_Object) { // 分别获取年、月、日、时、分并附加到字符串。 var date_String = date_Object.getFullYear() + "/" + (date_Object.getMonth() + 1) + "/" + +date_Object.getDate() + " " + +date_Object.getHours() + ":" + +date_Object.getMinutes(); return date_String; } var new_date = new Date(); // 调用 date_TO_String 函数 var date_string = date_TO_String(new_date); console.log("The date string is " + date_string);
输出
上述代码将产生以下输出 -
The date string is 2022/12/14 10:47
本教程教了我们三种将 Date 对象转换为所需字符串格式的方法。在本教程的前两部分中,我们使用了 Date 类的不同方法,这些方法不允许我们完全灵活地格式化日期字符串。我们可以使用上面学到的自定义函数根据我们的要求格式化日期字符串。