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

时间:2015-11-12 10:16:08

标签: javascript node.js datetime timezone momentjs

此代码使用node.js moment-timezone获取纽约时间。

set.seed(1)
df<-as.data.frame(matrix(rnorm(100),10)) # reproducible data
paste0('col1 vs col',2:10)->column1 # first column : the regression
sapply(2:10,function(x){summary(lm(df[,1]~df[,x]))$r.squared})->column2 # the rsquared
final<-data.frame('reg'=column1,'rsquared'=column2) # the final data.frame

        final
            reg   rsquared
1  col1 vs col2 0.14190543
2  col1 vs col3 0.51242469
3  col1 vs col4 0.05973700
4  col1 vs col5 0.05149017
5  col1 vs col6 0.37621382
6  col1 vs col7 0.14208468
7  col1 vs col8 0.38533983
8  col1 vs col9 0.26596917
9 col1 vs col10 0.01758616

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

我想格式化2015-11-12T05:09:49-05:00,使其格式为(HH:MM),看起来像time_output

3 个答案:

答案 0 :(得分:3)

只需将.format('zz')更改为.format('hh:mm')即可。 这应该有用。

答案 1 :(得分:2)

以下工作:

$(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>

编辑:

有关格式选项的详细信息,请查看momentjs docs

编辑:

轻微差异:

  • h - 24小时时间
  • H - 与A
  • 一起使用的12小时时间

致Matt Johnson

答案 2 :(得分:2)

@Margus gets credit for this one having found the docs specific to the case here: http://momentjs.com/docs/#/parsing/

Your specific problem can be solved with code like this:

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

For future info hunting where docs are lacking the unit tests often provide useful insights into library use cases.

The tests for momentjs provide a useful aet of examples: https://github.com/moment/moment/blob/develop/src/test/moment/format.js

If no unit tests are available (uh oh!) or you need clarification the source code can be referred to. It just so happens that momentjs source is pretty nicely structured for this kind of discovery.

The date formats are in source as a regex: https://github.com/moment/moment/blob/develop/src/lib/format/format.js#L3