时刻js日期时间比较

时间:2018-06-08 20:55:40

标签: javascript momentjs

我正在从javascript函数进行API调用,该函数正确返回JSON响应。但是当我逐步执行JSON响应时,当我点击moment().isSameOrBefore时,我收到了弃用警告以及Function.createFromInputFallback(moment.js line:320)中的错误。我是javascript的新手,尤其是Node的时刻包。

基本上,我想确定哪个预测潮汐最接近当前时间执行此功能。这是使用moment().isSameOrBefore参数的正确方法和/或我应该修改代码以区别对待这个问题吗?

以下是JSON:

{ "predictions" : [ {"t":"2018-06-08 03:06", "v":"3.795", "type":"H"},{"t":"2018-06-08 09:25", "v":"0.443", "type":"L"},{"t":"2018-06-08 15:51", "v":"3.884", "type":"H"},{"t":"2018-06-08 22:01", "v":"0.778", "type":"L"} ]}

这是我的功能:

const getTide = require('./modules/getTide.js');

var moment = require('moment');

async function testMod() {
    await getTide.getQuote().then(function(value){
        const parsedData = JSON.parse(value);       
        let text = " "; 
        // This loop steps through the JSON response row by row to test the data
        var i;
        for (i = 0; i < parsedData.predictions.length; i++) { 
            text += 'Record #' + i + ' = ' + parsedData.predictions[i].t + " " + parsedData.predictions[i].v + " " + parsedData.predictions[i].type + " - ";            
            let curDateTime = moment().format('LLL');           
            let theDate = moment(parsedData.predictions[i].t).format('LLL'); 
            let fromNow = moment(parsedData.predictions[i].t).fromNow(); 
            if (parsedData.predictions[i].type === "H") {
                    console.log('High tide for ' + parsedData.predictions[i].t + ', at ' + parsedData.predictions[i].v + ' vertical Feet. ');                                  
                    if (moment(theDate).isSameOrBefore(curDateTime)) {
                       console.log('It is currently ' + curDateTime + ' and that high tide was ' + fromNow);
                    } else {
                       console.log('It is currently ' + curDateTime + ' and that high tide is ' + fromNow + ' from now!');  
                    }                   
                  } else {
                    console.log('Low tide for ' + parsedData.predictions[i].t + ', at ' + parsedData.predictions[i].v + ' vertical Feet. ');
                    if (moment(theDate).isSameOrBefore(curDateTime)) {
                       console.log('It is currently ' + curDateTime + ' and that low tide was ' + fromNow);
                    } else {
                       console.log('It is currently ' + curDateTime + ' and that low tide is ' + fromNow + ' from now!');   
                    }               
                  }     
        }
    }, function(error){
        console.log("problem");
    });
}

testMod();

1 个答案:

答案 0 :(得分:1)

我认为问题的一部分是您使用格式化字符串来创建时刻实例,而不仅仅是使用时刻实例本身。所以不要这样做:

let curDateTime = moment().format('LLL');           
let theDate = moment(parsedData.predictions[i].t).format('LLL'); 
// ...
moment(theDate).isSameOrBefore(curDateTime);

尝试:

let curDateTime = moment();
let theDate = moment(parsedData.predictions[i].t);
// ...
theDate.isSameOrBefore(curDateTime); // because theDate is now a moment instance, you can call this function on it

当处理片刻时,将日期时间存储为片刻实例总是一个好主意,直​​到您需要将它们显示给用户,然后您可以继续:

theDate.format('LLL');

我认为你得到的警告是因为你试图在&#39; LLL&#39;中创建一个带有字符串的时刻实例。那个时刻抱怨的格式(&#34;弃用警告:提供的值不是公认的RFC2822或ISO格式。&#34;是我看到的警告)。如果要解析这些格式,还必须指定格式:

moment('September 4, 1986 8:30 PM', 'LLL').format('YYYY-MM-DD H:mm'); // won't complain