大家好,我是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");
}
});
輸出 - 會議在未來
問題 - 使用此代碼我得到了meetings in future,但所有會議時間實際上都是過去的時間。我該如何解決這個問題?
uj5u.com熱心網友回復:
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 上輕松找到更多代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/407982.html
標籤:
