简体   繁体   English

Moment JS日期时间比较

[英]Moment js date time compare

I'm making an API call from a javascript function which is returning a JSON response correctly. 我正在从javascript函数进行API调用,该函数正确返回了JSON响应。 But when stepping through the JSON response I'm getting a deprecation warning as well as an error at Function.createFromInputFallback (moment.js line:320) when I hit the line with moment().isSameOrBefore . 但是,当我逐步执行JSON响应时,当我使用moment().isSameOrBefore行时,我收到了弃用警告以及Function.createFromInputFallback(moment.js line:320) moment().isSameOrBefore I'm fairly new to javascript and especially the moment package for Node. 我对javascript还是相当陌生的,尤其是Node的即时包。

Basically, I would like to establish which predicted tide is the closest to the current time when this function is executed. 基本上,我想确定在执行此功能时哪个预测潮汐最接近当前时间。 Is this the correct way to use the moment().isSameOrBefore paramter and/or should I modify the code to go about this differently? 这是使用moment().isSameOrBefore参数的正确方法,并且/或者我是否应该修改代码以进行不同的处理?

Here's the JSON: 这是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"} ]}

Here's my function: 这是我的功能:

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

I think part of the issue is you're using formatted strings to create moment instances instead of just using the moment instance itself. 我认为部分问题是您正在使用格式化的字符串创建矩实例,而不是仅使用矩实例本身。 So instead of doing this: 所以不要这样做:

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

Try: 尝试:

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

When working with moment, it's always a good idea to keep your datetimes stored as moment instances until you need to display them to the user, then you can go ahead and do: 在使用矩时,将日期时间存储为矩实例一直是一个好主意,直​​到需要将其显示给用户为止,然后可以继续执行以下操作:

theDate.format('LLL');

I think the warning you are getting is because you are trying to create a moment instance with a string in the 'LLL' format which moment is complaining about ("Deprecation warning: value provided is not in a recognized RFC2822 or ISO format." is the warning I am seeing). 我认为您收到的警告是因为您正在尝试创建一个时刻实例,该实例带有一个'LLL'格式的字符串,该字符串正在引起抱怨(“弃用警告:提供的值未采用公认的RFC2822或ISO格式。”我看到的警告)。 If you wanted to parse these formats you have to specify the format as well: 如果要解析这些格式,则还必须指定格式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM