关于流星的Moment.js,时区的问题

时间:2018-02-17 02:36:56

标签: javascript node.js meteor momentjs

我一直试图用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://本地主机:3000 /包/ blaze.js散列= 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://本地主机:3000 /应用/客户/视图/页/ template.home.js哈希= be956f7036145947c4a904d9c42627576e740dd1:82:14   doRender @ http://localhost:3000/packages/blaze.js?hash=a1ff2d6d5ecd59ee11e2ba260b8650a9d1140f59:2086:20   viewAutorun / HTTP://本地主机:3000 /包/ blaze.js散列= 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 npmmeteor add

重新安装时刻和momnet-timezone

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

2 个答案:

答案 0 :(得分:0)

我不确定这里的问题是什么,但你可以尝试另一种方法来创建一个时间点。使用函数<html> <head> <style> #outer { height:100%; margin:0px auto; border: 1px solid; background: url("https://preview.ibb.co/gYykLn/images.jpg"); height: auto; width:auto; background-position: center; background-repeat: no-repeat; background-size: cover; } #form { position: relative; height: auto; width: 250px; left: 70%; bottom: 10%; background-color: rgba(0,0,0,0.6); border-color: #ffa200; border-width: 50px; border-radius: 20px; padding-bottom:10px; } .logo { height: 32px; width: 32px; position: relative; top: 10px; left:0px; margin-left: 10px; } .lgo { position: absolute; right: 215px; top: 56px; } .first { height: 40px; background-color: rgba(0,0,0,0.8); color: white; width: 180px; border-radius:5px; margin-top:40px; } .a { height: 40px; background-color: rgba(0,0,0,0.8); color: white; width: 180px; border-radius:5px; margin-top:20px; } .a:hover { border-color: #ffa200; } .first:hover { border-color: #ffa200; } .b { font-size: 20px; color: white; height: 40px; width: 180px; border-radius:5px; background-color: #ffa200; border-radius: 5px; margin-top:10px; margin-left:30px; } .b:hover { background-color: #dd8c00; } </style> </head> <body> <div id="outer"> <div id="form"> <form> <img class="logo" src="https://image.ibb.co/bAvSi7/download.png" > <input class="a first" type="text" name="regno" required/> <img class="logo" src="https://image.ibb.co/jpgGRS/l1.png" > <input class="a " type="text" name="name" placeholder="Enter your name" required/> <img class="logo" src="https://image.ibb.co/cSWuD7/l3.png" > <input class="a" type="email" name="email" placeholder="Enter your email" required/> <img class="logo" src="https://image.ibb.co/eeGSY7/l2.png" > <input class="a" type="password" name="pass" placeholder="Enter your password" required/> <img class="logo" src="https://image.ibb.co/eeGSY7/l2.png" > <input class="a" type="password" name="password" placeholder="Confirm your password" required/> <img class="logo" src="https://image.ibb.co/bKqX0n/l4.jpg" > <input class="a" type="tel" name="mobile" placeholder="Enter your mobile number" required/> <input class="b" type="submit" value="Register"/> </form> </div> </div> </body> </html>,您可以传递日期和时区。

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;
})();

答案 1 :(得分:0)

我正在挖掘文档,但我无法找到.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
}