gpt4 book ai didi

javascript - 检查字符串格式的日期时间是否等于中午

转载 作者:行者123 更新时间:2023-12-02 22:24:52 25 4
gpt4 key购买 nike

我有一个对象数组,其中每个对象都有一个日期时间作为一个值:

[
{dt: "2019-11-29 12:00:00"},
{dt: "2019-11-29 3:00:00"},
{dt: "2019-11-29 6:00:00"},
{dt: "2019-11-30 12:00:00"},
{dt: "2019-11-30 6:00:00"}
]

我只想返回所有带有时间 12:00:00 的日期。

最佳答案

您可以使用 JavaScript 的 array filter function迭代所有日期对象。对于每个对象,首先将日期字符串转换为 internal date representation然后检查时、分、秒是否适合中午。

const array = [
{dt: "2019-11-29 12:00:00"},
{dt: "2019-11-29 3:00:00"},
{dt: "2019-11-29 6:00:00"},
{dt: "2019-11-30 12:00:00"},
{dt: "2019-11-30 6:00:00"}
];

const result = array.filter((stringDate) => {
const rawDate = new Date(stringDate.dt);
return rawDate.getHours() === 12 && rawDate.getMinutes() === 0 && rawDate.getSeconds() === 0;
});
console.info(result);

关于javascript - 检查字符串格式的日期时间是否等于中午,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59103409/

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