JavaScript Date 对象 toLocaleString() 方法


定义和用法

toLocaleString() 方法可根据本地时间把 Date 对象转换为字符串,并返回结果。

语法
  1. dateObject.toLocaleString()
返回值

dateObject 的字符串表示,以本地时间区表示,并根据本地规则格式化。


实例

例子 1

在本例中,我们将根据本地时间把今天的日期转换为字符串:

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. var d = new Date()
  5. document.write(d.toLocaleString())
  6. </script>
  7. </body>
  8. </html>
例子 2

在本例中,我们将根据本地时间把具体的日期转换为字符串:

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. var born = new Date("July 21, 2021 01:15:00")
  5. document.write(born.toLocaleString())
  6. </script>
  7. </body>
  8. </html>

分类导航