[英]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;
}
});
我在使用此日志时获得的日志如下:
2018年2月17日,星期六,上午1:11 home.js:141:5 America / Vancouver home.js:142:5模板助手中的异常: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 BlazeException__wrapC :/ 3000 /blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:1715:14 wrapHelper / HTTP://本地主机:3000 /包/ blaze.js哈希= a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3103:14 Template._withTemplateInstanceFunc @ HTTP://本地主机:3000 /包/火焰的.js散列= a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3744:12 wrapHelper / <@ HTTP://本地主机:3000 /包/ blaze.js散列= a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:3102:12 Spacebars.call @ HTTP://本地主机:3000 /包/ spacebars .js?hash = 547cf8e466d1d52603d19bd5f48fb5df184fd237:172:12 Spacebars.mustacheImpl @ http:// localhost:3000 / packages / spacebars.js?h ash = 547cf8e466d1d52603d19bd5f48fb5df184fd237:106: 10Spacebars。 template.home.js?hash = be956f7036145947c4a904d9c42627576e740dd1:82:14 doRender @ http:// localhost:3000 / packages / blaze.js?hash = a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:2086:20 viewAutorun // http://blaze.3000 js?hash = a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:1934:18 Template._withTemplateInstanceFunc @ http:// localhost:3000 / packages / blaze.js?hash = a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59 / 3 :// Package / package @ 3:12:12 。 js?hash = a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:1932:14 Blaze._withCurrentView @ http:// localhost:3000 / packages / blaze.js?hash = a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59 / 227 :// bla? 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 = 0e8b5c18d183a28ce26b492 :9 Tracker._runFlush @ http:// localhost:3000 / packages / tracker.js?hash = 0e8b5c18d543a28ce43b2f183c26b49ee62196af:532:9 onGlobalMessage @ http:// localhost:3000 / packages / meteor.js?hash = b0f12795c8cc1423b5850502871996903fed5905
我尝试使用meteor npm
和meteor add
重新安装时刻和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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.