使用momentjs,在Azure上获得不同的结果

他testing代码:

var c = moment().tz('America/New_York'); console.log('c.format: ' + c.format()); var b = moment([c.year(), c.month(), c.date(), c.hours(), c.minutes()]).tz('America/Chicago'); console.log("b.format: " + b.format()); 

当我在本地运行这个代码时,我得到:

 c.format: 2017-07-03T16:33:42-04:00 b.format: 2017-07-03T16:33:00-05:00 

这是我期望(和想要)发生的事情。 基本上我只是想花一些时间,改变偏移而不改变实际的时间。 但是,当我通过Azure托pipe应用运行相同的代码时,输​​出是这样的:

 c.format: 2017-07-03T16:43:16-04:00 b.format: 2017-07-03T11:43:00-05:00 

本地和Azure应用程序都运行相同的节点版本(8.0.0)以及时刻(2.18.1)和瞬时时区(0.5.13)。

任何人有任何想法可能会导致这一点? 谢谢!

正如文件所说:

默认情况下,瞬间parsing并显示当地时间。

对于你的bvariables,你正在使用c.year(), c.month(), c.date(), c.hours(), c.minutes()作为本地时间创build一个c.year(), c.month(), c.date(), c.hours(), c.minutes()对象,所以转换结果bAmerica/Chicago时区将取决于系统。

您可以使用moment.tz来创build一个指定时区的时刻对象(例如America/New_York ),在您的情况下,如下所示:

 moment.tz([c.year(), c.month(), c.date(), c.hours(), c.minutes()], 'America/New_York') 

这里是一个在不同情况下显示实时结果的片段:

 // Current time in New York var c = moment().tz('America/New_York'); console.log('c.format: ' + c.format()); // Create a local moment object for the current time in New York var mLocal = moment([c.year(), c.month(), c.date(), c.hours(), c.minutes()]); console.log("mLocal.format: " + mLocal.format()); // Convert local moment to America/Chicago timezone var b = mLocal.tz('America/Chicago'); console.log("b.format: " + b.format()); // Create moment object for the current time in New York // specifying timezone and then converting to America/Chicago timezone var b1 = moment.tz([c.year(), c.month(), c.date(), c.hours(), c.minutes()], 'America/New_York').tz('America/Chicago'); console.log("b1.format: " + b1.format()); 
 <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.min.js"></script>