如何以12小时格式(AM/PM)显示JavaScript datetime对象?
当前回答
查看Datejs。它们的内置格式化程序可以做到这一点:http://code.google.com/p/datejs/wiki/APIDocumentation#toString
这是一个非常方便的库,特别是如果您打算用date对象做其他事情时。
其他回答
如果你不需要打印上午/下午,我发现以下内容很好很简洁:
var now = new Date();
var hours = now.getHours() % 12 || 12; // 12h instead of 24h, with 12 instead of 0.
这是基于@bbrame的回答。
下面是一种使用regex的方法:
console.log(新日期(“7/10/2013 20:12:34”).toLocaleTimeString () .replace (/ ((\ d) +: \ d {2}) (: \ [d] {2 })(.*)/, "$ 1美元3”)) console.log(新日期(“7/10/2013 01:12:34”).toLocaleTimeString () .replace (/ ((\ d) +: \ d {2}) (: \ [d] {2 })(.*)/, "$ 1美元3”))
这将创建3个匹配组:
([\d]+:[\d]{2}) -小时:分钟 (:[\d]{2}) -秒 (.*) -空格和时段(时段是AM/PM的正式名称)
然后显示第1组和第3组。
警告:toLocaleTimeString()可能会根据区域/位置表现不同。
希望这个答案比其他答案更容易理解(尤其是对新手来说)。
以下是我在一些站点中实现的解决方案,用于通知上次修改站点代码的时间。它通过date的选项参数实现AM/PM时间。toLocaleDateString(参见相关的Mozilla文档)。
// Last time page code was updated/changed
const options = {
year: "numeric",
month: "long",
weekday: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: true // This is the line of code we care about here
/*
false: displays 24hs format for time
true: displays 12, AM and PM format
*/
};
let last = document.lastModified;
let date = new Date(last);
let local = date.toLocaleDateString("en-US", options);
let fullDate = `${local}`;
document.getElementById("updated").textContent = fullDate;
输出的格式是:
Saturday, May 28, 2022, 8:38:50 PM
该输出然后显示在以下HTML代码中:
<p>Last update: <span id="updated">_update_date_goes_here</span></p>
注意:在这个用例中,是文档。lastModified有一些奇怪的行为,这取决于它是在本地运行还是在外部服务器上运行(参见这个堆栈溢出问题)。虽然当我在我的GitHub页面上运行它时,它可以正常工作(你应该在网站的页脚处看到它在运行)。
这是我的解决方案
function getTime() {
var systemDate = new Date();
var hours = systemDate.getHours();
var minutes = systemDate.getMinutes();
var strampm;
if (hours >= 12) {
strampm= "PM";
} else {
strampm= "AM";
}
hours = hours % 12;
if (hours == 0) {
hours = 12;
}
_hours = checkTimeAddZero(hours);
_minutes = checkTimeAddZero(minutes);
console.log(_hours + ":" + _minutes + " " + strampm);
}
function checkTimeAddZero(i) {
if (i < 10) {
i = "0" + i
}
return i;
}
或者只是简单地执行以下代码:
<script>
time = function() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt_clock').innerHTML = h + ":" + m + ":" + s;
var t = setTimeout(function(){time()}, 0);
}
time2 = function() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
if (h>12) {
document.getElementById('txt_clock_stan').innerHTML = h-12 + ":" + m + ":" + s;
}
var t = setTimeout(function(){time2()}, 0);
}
time3 = function() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
if (h>12) {
document.getElementById('hour_line').style.width = h-12 + 'em';
}
document.getElementById('minute_line').style.width = m + 'em';
document.getElementById('second_line').style.width = s + 'em';
var t = setTimeout(function(){time3()}, 0);
}
checkTime = function(i) {
if (i<10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
</script>
推荐文章
- 如何将python datetime转换为字符串,具有可读格式的日期?
- JavaScript: override alert()
- 重置setTimeout
- 如何确保<select>表单字段被禁用时提交?
- jQuery有不聚焦的方法吗?
- 在c#中创建一个特定时区的DateTime
- 反应钩子-正确的方式清除超时和间隔
- TypeScript枚举对象数组
- 在React.js中正确的img路径
- 在React.js中更新组件onScroll的样式
- onClick ReactJS调用多个函数
- 如何在python中验证日期字符串格式?
- 如何在JavaScript中转义单引号(')?
- Ng-repeat结束事件
- 谷歌MAP API未捕获的类型错误:无法读取属性“offsetWidth”为空