我很难得到片刻()。到()和片刻()。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'
。
无法在JSFiddle中重现这一点 - 工作正常。我在Parse和小提琴上使用相同版本的moment.js。有什么想法吗?
答案 0 :(得分:0)
我解决了 - 我错误地使用了utcOffset。
我设法通过删除now.utcOffset(utcOffset);
并将其替换为now.add(utcOffset - now.utcOffset(), 'minutes');
如果有更好的答案,我希望看到它......