Javascript: moment().to() 似乎不适用于 Parse 上的 moment().utcOffset()



我有麻烦得到moment().to()和moment(). utcoffset()很好地放在一起解析云代码。

我在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;
}

问题是,即使'现在',' opentimemmoment '和' closetimemmoment '显示正确的,utc调整的时间,'to'函数似乎忽略了它的'现在'。例如,我得到这样的输出(当时间是15:00,并且我已经检查了now是正确的)'Currently open, closes in 10 hours at 17:00'

如果不包括8小时的utc调整,

10小时是正确的。但显然应该是2小时。

无法在JSFiddle中复制此内容-工作正常。我在Parse上使用的moment.js版本与小提琴上使用的相同。什么好主意吗?

我解决了这个问题-我错误地使用了utcOffset

我设法纠正我的功能通过删除now.utcOffset(utcOffset);和替换它与now.add(utcOffset - now.utcOffset(), 'minutes');

如果有更好的答案,我想看看……

相关内容

  • 没有找到相关文章

最新更新