首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >时刻js日期时间比较

时刻js日期时间比较
EN

Stack Overflow用户
提问于 2018-06-09 04:55:40
回答 1查看 61关注 0票数 0

我从一个javascript函数调用API,它正确地返回了一个JSON响应。但是,在单步执行JSON响应时,当我使用moment().isSameOrBefore命中该行时,我在Function.createFromInputFallback (moment.js行:320)处收到了一个弃用警告和一个错误。我对javascript是个新手,尤其是Node的moment包。

基本上,我想确定在执行此函数时,哪个预测潮汐最接近当前时间。这是使用moment().isSameOrBefore参数的正确方式吗?或者,我是否应该修改代码,以不同的方式执行此操作?

这是

代码语言:javascript
运行
复制
{ "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"} ]}

下面是我的函数:

代码语言:javascript
运行
复制
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();
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50768285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档