[英]How to compare two different date formats in javascript
大家好,我是 javascript 中日期时间的新手。 在比较两种不同的日期格式时,我面临一个问题。 目标 - 我想将来自后端的日期/时间与当前时间进行比较。 如果来自后端的时间已经过去,我需要做一些其他的事情,或者如果是将来我想做其他事情。
问题 - 当前日期格式是这样的 - Mon Jan 10 2022 16:38:58 GMT+0530 (India Standard Time)
从后端获取的日期时间就像 - 2022-01-03T18:30:00Z
代码 -
$scope.getTimeDifference = function(meetingsData){
meetingsData.forEach(function (arrayItem) {
var currentTime = new Date();
var x = arrayItem.meetingTime;
console.log(x);
console.log(currentTime)
if(x < currentTime){
console.log("meeting is in the past");
}
else{
console.log("Meeting is in future");
}
});
Output - 会议在未来
问题 - 使用此代码我将meetings in future
,但所有会议时间实际上都是过去的时间。 我该如何解决这个问题?
new Date
将采用任何一种格式。
const d1 = new Date("Mon Jan 3 2022 16:38:58 GMT+0530 (India Standard Time)") const d2 = new Date("2022-01-03T18:30:00Z") console.log(d1) console.log(d2) if (d1 < d2) console.log("Date 1 is earlier than d2") // To find hh:mm:ss difference for the same day we can do this. console.log(new Date(d2-d1).toISOString().substr(11, 8))
如果您想要天数、小时数等方面的差异,您将需要在 SO 上轻松找到更多代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.