从时区转换中提取时间(HH:MM)

这段代码使用node.js的moment-timezone来获取纽约时间。

var moment = require('moment-timezone'); var time_output= moment().tz("America/New_York").format(); 

time_output看起来像这样2015-11-12T05:09:49-05:00

我想格式化time_output ,使其格式(HH:MM),看起来像05:09

@Margus得到这个在这里find具体案件的文档信用: http ://momentjs.com/docs/#/parsing/

你的具体问题可以用这样的代码解决:

 var moment = require('moment-timezone'); var time_output= moment().tz("America/New_York").format("HH:mm"); 

对于未来的信息search缺乏文档,unit testing通常会为图书馆用例提供有用的见解。

momentjs的testing提供了一个有用的例子: https : //github.com/moment/moment/blob/develop/src/test/moment/format.js

如果没有unit testing可用(呃哦!)或者你需要澄清源代码可以参考。 恰恰相反,这个发现的时代源泉是非常好的结构。

date格式在源代码中是一个正则expression式: https : //github.com/moment/moment/blob/develop/src/lib/format/format.js#L3

只要将你的.format('zz')改为.format('hh:mm') 。 这应该工作。

以下将工作:

 $(function() { var datestring = moment().tz("America/New_York").format("HH:mm"); $("#result").append(datestring); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <script src="http://momentjs.com/downloads/moment-with-locales.min.js"></script> <script src="http://momentjs.com/downloads/moment-timezone-with-data-2010-2020.min.js"></script> <div id="result"><div>