[英]Change date to 24 hours format in JS
I have the following date variable: const datum = new Date("2022-09-04T22:07:56+02:00")
Which gives me the date: "2022-09-04T22:07:56+02:00"
.我有以下日期变量:
const datum = new Date("2022-09-04T22:07:56+02:00")
这给了我日期: "2022-09-04T22:07:56+02:00"
. I now want to format this date to look like the following: "04.9.2022 22:07:56"
.我现在想将此日期格式化为如下所示:
"04.9.2022 22:07:56"
。
I was trying to use Moment.js which worked only partly:我试图使用仅部分起作用的 Moment.js:
datum = moment(datum, 'YYYY-M-DD hh:mm:ss').format('DD.M.YYYY hh:mm:ss')
Which gives me the following: "04.9.2022 10:07:56"
this would be correct if we would use the 12 hour clock, but we are using the 24 hour clock, so I need this instead: "04.9.2022 22:07:56"
这给了我以下信息:
"04.9.2022 10:07:56"
如果我们使用 12 小时制,这将是正确的,但我们使用的是 24 小时制,所以我需要这个: "04.9.2022 22:07:56"
Is there a way to change that the way I need it to be?有没有办法改变我需要的方式?
You may try my code:你可以试试我的代码:
const now = new Date("2022-09-04T22:07:56+02:00"); let options = { month:"2-digit", day:"2-digit", hourCycle: 'h23', hour: "2-digit", minute: "numeric", second: "numeric", year:"numeric" } let result=now.toLocaleTimeString("en-GB", options); result=result.replaceAll("/","."); result=result.replace(",",""); console.log(result);
Try changing 'hh' to 'HH'.尝试将“hh”更改为“HH”。
moment(new Date("2022-09-04T22:07:56+02:00"),'YYYY-M-DD HH:mm:ss').format("DD.M.YYYY HH:mm:ss")
You can pass the offset value to retain the input offset value
您可以传递偏移值以保留输入的偏移值
let datum = new Date("2022-09-04T22:07:56+02:00"); let offset = moment.parseZone("2022-09-04T22:07:56+02:00").utcOffset(); let finalDate = moment(datum).utcOffset(offset).format("DD.M.YYYY HH:mm:ss"); console.log(finalDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
change hh to HH将 hh 更改为 hh
moment(new Date("2022-09-04T22:07:56+02:00"),'YYYY-M-DD HH:mm:ss').format("DD.M.YYYY HH:mm:ss")时刻(新日期("2022-09-04T22:07:56+02:00"),'YYYY-M-DD HH:mm:ss').format("DD.M.YYYY HH:mm:ss" )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.