-1

I have an ISO-8601 date of the form:

2014-02-02T15:00:00-0800

Can anyone demonstrate how to to extract a time of the form

3:00pm

Using moment.js

Ben Pearce
  • 6,884
  • 18
  • 70
  • 127

1 Answers1

3

Here you find Parse documentation. Here is Format. Combined together gives:

var dateAsString = '...';
moment.utc(dateAsString).format('h:mmA');

http://jsfiddle.net/76Ued/1/
http://jsfiddle.net/76Ued/2/ <- this one uses local()

EDIT
Version considering Matt Johnson's hint:

var dateAsString = '...';
moment(dateAsString).format('h:mmA');

http://jsfiddle.net/76Ued/13/

SOReader
  • 5,697
  • 5
  • 31
  • 53
  • Thanks for your answer. The formatting looks good but I'm getting unexpected results. For example I expected the output time in your example be 3:00pm and not 11:00pm. – Ben Pearce Jan 20 '14 at 23:50
  • 1
    3pm is in your local time which is 11pm in Greenwich time. I think `.local()` prior to format will help. More info: http://momentjs.com/docs/#/manipulating/local/ – SOReader Jan 21 '14 at 00:02
  • 2
    Just do `moment(dateAsString)`. There's no need to parse as utc if you're just going to convert back to local again. – Matt Johnson-Pint Jan 22 '14 at 03:57
  • @MattJohnson Thanks Matt, you should post it as an alternative answer while it answers better the question, imho. – SOReader Jan 22 '14 at 09:21