Moment js 日期時間對比 - Moment js date time comparison

問題:

I'm using moment.js to format my date time, here I have two date values, and I want to achieve a particular function when one date is greater than the other.我正在使用 moment.js 來格式化我的日期時間,這裏我有兩個日期值,我想在一個日期大於另一個日期時實現特定功能。 I read most of their docs, but didn't find the function to achieve this.我閱讀了他們的大部分文檔,但沒有找到實現此目的的功能。 I know it will be there.我知道它會在那裏。

This is my code:這是我的代碼:

var date_time = 2013-03-24 + 'T' + 10:15:20:12 + 'Z'
var d = moment(date_time).tz('UTC'); // first date 

var now = new Date(),
    dnow = moment(now).tz('UTC'),
    snow = dnow.minute() % 15,
    diffnow = 15 - snow,
    tonow = moment(dnow).add('minute', diffnow),
    ahead30now = moment(tonow).add('minute', 30);

if (d > ahead30now) {
    // allow input time
    console.log('UTC TIME DB', d.format());
} else {

}

Edit編輯

var date_time = req.body.date + 'T' + req.body.time + req.body.timezone; // 2014-03-24T01:15:000
var utc_input_time = moment(date_time).utc(); // 2014-03-24T01:15:000
console.log('utc converted date_time', moment(date_time).utc().format("YYYY-MM-DDTHH:mm:SSS"));
var isafter = moment(utc_input_time).isAfter(moment('2014-03-24T01:14:000')); // true
if(isafter === true){
    console.log('is after true');
} else {
    console.log('is after is false');
}

Here, I am comparing two dates ie 2014-03-24T01:15:000 > 2014-03-24T01:14:000 , expecting that the first one is greater than the second one, but it always goes to the else condition.在這裏,我比較兩個日期,即2014-03-24T01:15:000 > 2014-03-24T01:14:000 ,期望第一個大於第二個,但它總是進入 else 條件。 I don't know why?我不知道爲什麼?


解決方案:

參考一: https://stackoom.com/question/1WpVw
參考二: Moment js date time comparison
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章