簡體   English   中英

如何比較 javascript 中的兩種不同日期格式

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM