js怎么显示当前日期和时间
使用JavaScript可以通过以下方法显示当前日期和时间:
- 使用
Date
对象获取当前日期和时间:
var currentDate = new Date();
console.log(currentDate);
上述代码将打印出当前日期和时间,例如:Mon Jun 28 2021 15:30:00 GMT+0800 (China Standard Time)
。
- 使用
toLocaleString()
方法获取本地化的当前日期和时间:
var currentDate = new Date();
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' };
console.log(currentDate.toLocaleString('en-US', options));
上述代码将打印出经过格式化的当前日期和时间,例如:Monday, June 28, 2021, 15:30:00
.
- 使用
toLocaleDateString()
和toLocaleTimeString()
方法分别获取本地化的当前日期和时间:
var currentDate = new Date();
console.log(currentDate.toLocaleDateString());
console.log(currentDate.toLocaleTimeString());
上述代码将分别打印出经过格式化的当前日期和时间。
注意:以上方法获取的日期和时间都是根据用户计算机的时区和本地化设置来确定的。
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:Python爬虫代理池搭建的方法步骤 下一篇:PHP的stripos函数怎么使用
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。