代码之家  ›  专栏  ›  技术社区  ›  Hackbrew

时刻js日期时间比较

  •  0
  • Hackbrew  · 技术社区  · 8 年前

    我正在从一个javascript函数调用一个api,该函数正确地返回json响应。但是,当单步执行json响应时,当我使用 moment().isSameOrBefore . 我对javascript相当陌生,尤其是node的moment包。

    基本上,我想确定哪个预测潮汐最接近执行此函数时的当前时间。这是正确的使用 力矩().IsSameorFore parameter和/或我应该修改代码以进行不同的操作吗?

    以下是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 回复  |  直到 8 年前
        1
  •  1
  •   aStewartDesign    8 年前

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

    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');
    

    我想你得到的警告是因为你试图用moment抱怨的'lll'格式的字符串创建一个moment实例(“deprecation warning:value provided is not in a recognized rfc2822 or iso format.”是我看到的警告。如果要分析这些格式,还必须指定格式:

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