如何使用当前语言环境的约定以字符串形式返回Date的“时间”部分?
若要使用当前语言环境的约定以字符串形式返回Date的“时间”部分,请使用toLocaleTimeString()
方法。
toLocaleTimeString方法在格式日期上依赖于基础操作系统。它将使用运行脚本的操作系统的格式约定将日期转换为字符串。例如,在美国,月份出现在日期(04/15/98)之前,而在德国,日期出现在月份(15.04.98)之前。
示例
您可以尝试运行以下代码以字符串形式返回Date的“时间”部分-
<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>