Javascript:moment()。to()似乎不适用于Parse

时间:2015-09-28 10:15:24

标签: javascript parse-platform timezone momentjs cloud-code

我很难得到片刻()。到()和片刻()。utcOffset()很好地放在Parse云代码上。

我在Parse.com上将其作为云代码运行,该代码始终为UTC。 (简化)代码是

function cafeOpenState(cafe, utcOffset) {
    var returnString;

    var now = moment();
    now.utcOffset(utcOffset);
    var dayStr = now.format('ddd');

    // Get appropriate open and closing times
    var openTime = Number(cafe.get('OpenTime')); // stored as 3-4 digit integer e.g. 830
    var closeTime = Number(cafe.get('CloseTime')); // stored as 3-4 digit integer e.g. 2130

    // Figure out what time it is now, in 0-2400 format
    var timeNowStr = now.format('Hmm'); // need this later
    var timeNow = Number(timeNowStr);

    // Find out where it is currently in the time range
    if (openTime == 0 && closeTime == 0) {
        returnString = 'Closed today';
    } else if (timeNow < openTime) {
        var openTimeMoment = moment(openTime, 'Hmm');
        var opensIn = now.to(openTimeMoment);
        var opensInStr = openTimeMoment.format('H:mm');
        returnString = 'Not yet open, opens ' + opensIn + ' at ' + opensInStr;
    } else if (timeNow > openTime && timeNow < closeTime) {
        var closeTimeMoment = moment(closeTime, 'Hmm');
        var closesIn = now.to(closeTimeMoment);
        var closesInStr = closeTimeMoment.format('H:mm');
        returnString = 'Currently open , closes ' + closesIn + ' at ' + closesInStr;
    } else if (timeNow > closeTime) {
        returnString = 'Closed for today';
    } else {
        returnString = 'error';
    }

    // Return a human readable string
    return returnString;
}

问题在于,即使现在&#39;,&#39; openTimeMoment&#39;和&#39; closeTimeMoment&#39;显示正确的UTC调整时间,&#39;到&#39;功能似乎忽略了它现在&#39;。例如,我得到的输出(当时间是15:00时,我已经检查now是否正确)'Currently open, closes in 10 hours at 17:00'

如果你不包括8小时的utc调整,那么10小时是正确的。但显然应该是2个小时。

无法在JSFiddle中重现这一点 - 工作正常。我在Parse和小提琴上使用相同版本的moment.js。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我解决了 - 我错误地使用了utcOffset。

我设法通过删除now.utcOffset(utcOffset);并将其替换为now.add(utcOffset - now.utcOffset(), 'minutes');

来更正我的功能

如果有更好的答案,我希望看到它......