如何使用当前语言环境的约定将日期的"时间"部分返回为字符串?
htmljavascriptprogramming scripts
要使用当前语言环境的约定将日期的"时间"部分返回为字符串,请使用 toLocaleTimeString() 方法。
toLocaleTimeString 方法依赖于底层操作系统来格式化日期。它使用运行脚本的操作系统的格式约定将日期转换为字符串。例如,在美国,月份出现在日期之前(04/15/98),而在德国,日期出现在月份之前(15.04.98)。
示例
您可以尝试运行以下代码以将日期的"时间"部分返回为字符串 −
<html> <head> <title>JavaScript toLocaleTimeString Method</title> </head> <body> <script> var dt = new Date(2018, 0, 15, 14, 16, 30); document.write( "Formated Date - Time : " + dt.toLocaleTimeString() ); </script> </body> </html>