gpt4 book ai didi

javascript - 如何在 12 小时内检索当前的 `DateTime`?

转载 作者:行者123 更新时间:2023-11-28 13:54:58 25 4
gpt4 key购买 nike

从 JavaScript 获取当前日期时间作为字符串的最佳方法是什么,格式如下:

11/5/2011 1:56:44 PM

而不是(24 小时/“军用”时间)

11/5/2011 13:56:44

最佳答案

演示:http://jsfiddle.net/k6g4g/

var date = new Date,
day = date.getDate(),
month = date.getMonth() + 1,
year = date.getFullYear(),
hour = date.getHours(),
minute = date.getMinutes(),
seconds = date.getSeconds(),
ampm = hour > 12 ? "PM" : "AM";

hour = hour % 12;
hour = hour ? hour : 12; // zero = 12

minute = minute > 9 ? minute : "0" + minute;
seconds = seconds > 9 ? seconds : "0" + seconds;
hour = hour > 9 ? hour : "0" + hour;


date = month + "/" + day + "/" + year + " " + hour + ":" + minute + ":" + seconds + " " + ampm;
// date holds "12/16/2011 08:14:30 PM"

关于javascript - 如何在 12 小时内检索当前的 `DateTime`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8538873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com