【问题标题】:Display specific date and time format in Javascript在 Javascript 中显示特定的日期和时间格式
【发布时间】:2022-03-06 05:08:36
【问题描述】:

我有以下代码来显示日期和时间

**<div id='time'></div>
<script type="text/javascript">
 
// get a new date (locale machine date time)
var date = new Date();
// get the date as a string
var n = date.toDateString();
// get the time as a string
var time = date.toLocaleTimeString();
// find the html element with the id of time
// set the innerHTML of that element to the date a space the time
document.getElementById('time').innerHTML = n + ' ' + time;
</script>**

显示为 Wed Feb 09 2022 5:20:44 PM

我正在寻找的是如何让它显示为:

2 月 9 日,星期三,下午 5:20

感谢您的帮助

谢谢

【问题讨论】:

标签: javascript date time


【解决方案1】:

这是一个简单的解决方案。

请注意,逗号位于工作日名称之后,因为这是正确的书写方式。但它可以很容易地更改为月份名称之后。

<div id='time'></div>
<script type="text/javascript">
 
// get a new date (locale machine datetime)

let date = new Date ();
let out = date.toLocaleString("en-GB",{weekday:'long',day:'numeric',month:'long'}) +" " +
          new Intl.DateTimeFormat("en-GB",{hour12:true,hour:'numeric',minute: 'numeric'}).format(date);


document.getElementById('time').innerHTML = out;
</script>

【讨论】:

    猜你喜欢
    • 2017-06-04
    • 1970-01-01
    • 2018-04-15
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多