Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

javascript - 检查字符串格式的日期时间是否等于正午(Check if string-formatted date time is equal to noon)

I have an array of objects where each object has a date time as one value:(我有一个对象数组,其中每个对象都有一个日期时间作为一个值:)

[ {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"} ] I want to return all only those dates with time 12:00:00 .(我只想返回所有时间为12:00:00日期。)   ask by MOFD translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use JavaScript's array filter function to iterate over all date objects.(您可以使用JavaScript的数组过滤器函数遍历所有日期对象。)

For each object, first convert the date string to an internal date representation and then check if the hour, minutes and seconds fit noon.(对于每个对象,首先将日期字符串转换为内部日期表示形式 ,然后检查小时,分钟和秒是否适合正午。) 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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...