【问题标题】:Moment.js on meteor, Problems with Timezone流星上的 Moment.js,时区问题
【发布时间】:2018-07-27 23:41:12
【问题描述】:

我一直在尝试找出 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 美国/温哥华 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 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 空格键.call@http://localhost:3000/packages/spacebars.js?hash=547cf8e466d1d52603d19bd5f48fb5df184fd237:172:12 空格键.mustacheImpl@http://localhost:3000/packages/spacebars.js?hash=547cf8e466d1d52603d19bd5f48fb5df184fd237:106:10 空格键.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 查看自动运行/http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:1932:14 Blaze._withCurrentView@http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:2271:12 查看自动运行@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 npmmeteor add 重新安装 moment 和 momnet-timezone

我知道这与 time.tz() 有关,传递给它的是以下America/Vancouver

【问题讨论】:

  • home.js 的第 143 行发生了什么?我猜你在哪里评论了休息时间。

标签: javascript node.js meteor momentjs


【解决方案1】:

虽然我找不到 .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
}

【讨论】:

    【解决方案2】:

    我不确定这里的问题是什么,但您可以尝试创建时区时刻的替代方法。使用函数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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 2013-04-12
      • 2016-08-06
      • 2019-04-02
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多