Moment.js 关于流星,时区问题
Moment.js on meteor, Problems with Timezone
我一直在试图找出 moment-timezone.js 的问题。我似乎无法将时间戳从 UTC 转换为 PST
我使用的辅助函数是 timeInTimeZone。请原谅糟糕的变量名。
非常感谢任何调试问题的帮助
Template.home.helpers({
timezone: function() {
return Template.instance().timezone.get();
},
timeInTimeZone : function(){
let time = moment( this.createdAt ),
format = 'dddd, MMMM Do YYYY h:mm a';
var timezone = Template.instance().timezone.get();
console.log(time.format( format )); //works
console.log(timezone); //works
var editedTime = time.tz( timezone ).format( format ); //breaks
console.log(editedTime);
return editedTime;
}
});
我使用这个得到的日志如下:
Saturday, February 17th 2018 1:11 am
home.js:141:5
America/Vancouver
home.js:142:5
Exception in template helper: timeInTimeZone@http://localhost:3000/app/client/views/pages/home.js?hash=f7d8b7711106080a4d6fbb1d2089cf344f59223d:143:22
bindDataContext/<@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3051:14
Blaze._wrapCatchingExceptions/<@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:1715:14
wrapHelper/http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3103:14
Template._withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3744:12
wrapHelper/<@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3102:12
Spacebars.call@http://localhost:3000/packages/spacebars.js?hash=547cf8e466d1d52603d19bd5f48fb5df184fd237:172:12
Spacebars.mustacheImpl@http://localhost:3000/packages/spacebars.js?hash=547cf8e466d1d52603d19bd5f48fb5df184fd237:106:10
Spacebars.mustache@http://localhost:3000/packages/spacebars.js?hash=547cf8e466d1d52603d19bd5f48fb5df184fd237:110:16
Template.homehttp://localhost:3000/app/client/views/pages/template.home.js?hash=be956f7036145947c4a904d9c42627576e740dd1:82:14
doRender@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:2086:20
viewAutorun/http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:1934:18
Template._withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3744:12
viewAutorun/<@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:1932:14
Blaze._withCurrentView@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:2271:12
viewAutorun@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:1931:12
Tracker.Computation.prototype._compute@http://localhost:3000/packages/tracker.js?hash=0e8b5c18d543a28ce43b2f183c26b49ee62196af:339:5
Tracker.Computation.prototype._recompute@http://localhost:3000/packages/tracker.js?hash=0e8b5c18d543a28ce43b2f183c26b49ee62196af:358:9
Tracker._runFlush@http://localhost:3000/packages/tracker.js?hash=0e8b5c18d543a28ce43b2f183c26b49ee62196af:532:9
onGlobalMessage@http://localhost:3000/packages/meteor.js?hash=b0f12795c8cc1423b5850502871996903f947ed5:448:11
我尝试使用 meteor npm
和 meteor add
重新安装 moment 和 momnet-timezone
我知道它与 time.tz() 有关,传递给它的是以下 America/Vancouver
我不确定这里的问题是什么,但您可以尝试创建时区时刻的替代方法。使用函数 moment.tz
,您可以传递日期和时区。
(function() {
/* ignore */
this.createdAt = moment();
const Template = {
instance: () => ({
timezone: { get: () => 'America/Vancouver' }
})
}
/* end */
let timezone = Template.instance().timezone.get();
let time = moment.tz(this.createdAt, timezone);
let format = 'dddd, MMMM Do YYYY h:mm a';
var editedTime = time.format(format);
console.log(editedTime);
return editedTime;
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone-with-data.min.js"></script>
虽然我找不到 .tz() 函数的合适解决方案,但我正在仔细研究文档
我最终使用了这段代码
timeInTimeZone : function(){
var date = moment.utc(this.createdAt); //take the date in UTC format
return date.local().format("ddd, MMM D YYYY h:mma"); //Return the date in user's local time
}
我一直在试图找出 moment-timezone.js 的问题。我似乎无法将时间戳从 UTC 转换为 PST
我使用的辅助函数是 timeInTimeZone。请原谅糟糕的变量名。
非常感谢任何调试问题的帮助
Template.home.helpers({
timezone: function() {
return Template.instance().timezone.get();
},
timeInTimeZone : function(){
let time = moment( this.createdAt ),
format = 'dddd, MMMM Do YYYY h:mm a';
var timezone = Template.instance().timezone.get();
console.log(time.format( format )); //works
console.log(timezone); //works
var editedTime = time.tz( timezone ).format( format ); //breaks
console.log(editedTime);
return editedTime;
}
});
我使用这个得到的日志如下:
Saturday, February 17th 2018 1:11 am home.js:141:5 America/Vancouver home.js:142:5 Exception in template helper: timeInTimeZone@http://localhost:3000/app/client/views/pages/home.js?hash=f7d8b7711106080a4d6fbb1d2089cf344f59223d:143:22 bindDataContext/<@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3051:14 Blaze._wrapCatchingExceptions/<@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:1715:14 wrapHelper/http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3103:14 Template._withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3744:12 wrapHelper/<@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3102:12 Spacebars.call@http://localhost:3000/packages/spacebars.js?hash=547cf8e466d1d52603d19bd5f48fb5df184fd237:172:12 Spacebars.mustacheImpl@http://localhost:3000/packages/spacebars.js?hash=547cf8e466d1d52603d19bd5f48fb5df184fd237:106:10 Spacebars.mustache@http://localhost:3000/packages/spacebars.js?hash=547cf8e466d1d52603d19bd5f48fb5df184fd237:110:16 Template.homehttp://localhost:3000/app/client/views/pages/template.home.js?hash=be956f7036145947c4a904d9c42627576e740dd1:82:14 doRender@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:2086:20 viewAutorun/http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:1934:18 Template._withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3744:12 viewAutorun/<@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:1932:14 Blaze._withCurrentView@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:2271:12 viewAutorun@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:1931:12 Tracker.Computation.prototype._compute@http://localhost:3000/packages/tracker.js?hash=0e8b5c18d543a28ce43b2f183c26b49ee62196af:339:5 Tracker.Computation.prototype._recompute@http://localhost:3000/packages/tracker.js?hash=0e8b5c18d543a28ce43b2f183c26b49ee62196af:358:9 Tracker._runFlush@http://localhost:3000/packages/tracker.js?hash=0e8b5c18d543a28ce43b2f183c26b49ee62196af:532:9 onGlobalMessage@http://localhost:3000/packages/meteor.js?hash=b0f12795c8cc1423b5850502871996903f947ed5:448:11
我尝试使用 meteor npm
和 meteor add
我知道它与 time.tz() 有关,传递给它的是以下 America/Vancouver
我不确定这里的问题是什么,但您可以尝试创建时区时刻的替代方法。使用函数 moment.tz
,您可以传递日期和时区。
(function() {
/* ignore */
this.createdAt = moment();
const Template = {
instance: () => ({
timezone: { get: () => 'America/Vancouver' }
})
}
/* end */
let timezone = Template.instance().timezone.get();
let time = moment.tz(this.createdAt, timezone);
let format = 'dddd, MMMM Do YYYY h:mm a';
var editedTime = time.format(format);
console.log(editedTime);
return editedTime;
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone-with-data.min.js"></script>
虽然我找不到 .tz() 函数的合适解决方案,但我正在仔细研究文档
我最终使用了这段代码
timeInTimeZone : function(){
var date = moment.utc(this.createdAt); //take the date in UTC format
return date.local().format("ddd, MMM D YYYY h:mma"); //Return the date in user's local time
}