-6

how to convert this date Thu, 16 Feb 2017 08:00:00 GMT to new format like hh:mm

my code so far :

var start = new Date(this.props.item.start); // this props returns Thu, 16 Feb 2017 08:00:00 GMT
var dateStr = start.format("hh:mm") 

any suggestions?

prosti
  • 42,291
  • 14
  • 186
  • 151
Mhmd Backer Shehadi
  • 559
  • 1
  • 12
  • 30

2 Answers2

0

Well for hours and minutes (hh:mm) you can use this:

var start = new Date(this.props.item.start); // this props returns Thu, 16 Feb 2017 08:00:00 GMT
var dateStr = start.getHours() + ':' + start.getMinutes();
Z-Bone
  • 1,534
  • 1
  • 10
  • 14
  • The string is not consistent with the [*format specified in ECMA-262*](http://ecma-international.org/ecma-262/7.0/index.html#sec-date-time-string-format) so parsing is implementation dependent. If parsed correctly, it will be considered UTC+0000, the *get\** methods will return times adjusted for the host timezone offset, which will likely differ between hosts. Lastly, the OP appears to want zero padding, so it doesn't return the required format. :-( – RobG Feb 03 '17 at 21:25
0

Parsing strings with the Date constructor (or Date.parse) is not recommended, see Why does Date.parse give incorrect results?

If all you want to do is reformat the string, then just do that and avoid the vagaries of date parsing:

function getTime(s) {
  return (s.match(/\d\d:\d\d/) || [])[0];
}

console.log(getTime('Thu, 16 Feb 2017 08:00:00 GMT'));
Community
  • 1
  • 1
RobG
  • 142,382
  • 31
  • 172
  • 209