2016-06-13 67 views
0

隨着Moment.js我試圖探測店開閉時間,但無論我多麼看看文檔和交換功能,圍繞我從isBefore() & isAfter()false響應提前Moment.js:鍛鍊店開閉時間

m.opening=[ 
    moment('11:30', "HH:MM"), 
    moment('01:30', "HH:MM") //1:30AM the next day 
]; 
console.log(m.opening[1].isBefore(moment('07:00', "HH:MM"))); //always false no matter if I use isBefore() or isAfter() 

if(m.opening[1].isBefore(moment('7:00', "HH:MM"))) 
    m.opening[1].add(1,'day'); //If closing time is before 7AM add a day 
    m.range = m.moment.range(m.opening); 

感謝您的幫助

+0

'時刻('01:30' , 「HH:MM」)// 1:30AM下一day' - 這是如何成爲第二天? –

+0

我希望用m.opening [1] .add(1,'day');'' – Ash

回答

2

你的價值沒有被設置爲你認爲他們是。

我添加了一個健全性檢查,發現m.opening是一個有兩個空值的數組。

其中,我讀了文檔,你傳遞錯誤的字符串來解析時間。你想要的字符串是HH:mm,而不是HH:MM

我在下面包含了一段代碼,包括代碼和一個工作示例。

雙重檢查值是你期望的值應始終是第一個調試步驟之一。

var m = {}; 
 

 
m.opening=[ 
 
    moment('11:30', "HH:MM"), 
 
    moment('01:30', "HH:MM") //1:30AM the next day 
 
]; 
 

 

 
console.log(m); 
 

 
console.log(m.opening[1].isBefore(moment('07:00', "HH:MM"))); //always false no matter if I use isBefore() or isAfter() 
 

 

 
////////////// 
 
m.opening=[ 
 
    moment('11:30', "HH:mm"), 
 
    moment('01:30', "HH:mm") //1:30AM the next day 
 
]; 
 

 

 
console.log(m); 
 

 
console.log(m.opening[1].isBefore(moment('07:00', "HH:mm"))); // Now returns true.
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>