Reputation: 73
I receive from a WebService a string with a date in this format yyyy-MM-ddTHH:mm:ss.fffZ. I need to convert it in a format like mmm dd hh mm using JavaScript. For example change the string "2017-02-08T09:19:47.550Z" to Feb 08 09:19.
How do I achieve this??
Upvotes: 0
Views: 1931
Reputation: 16779
While usually people do use Moment.js for complex date manipulation, this problem can be solved quite trivially with vanilla JS string manipulation and the Date#toUTCString
method.
function formatDate(date) {
var utc = date.toUTCString() // 'ddd, DD MMM YYYY HH:mm:ss GMT'
return utc.slice(8, 12) + utc.slice(5, 8) + utc.slice(17, 22)
}
console.log(
formatDate(new Date('2017-02-08T09:19:47.550Z'))
) //=> 'Feb 08 09:19'
Upvotes: 1
Reputation: 2068
Usually people use moment:
moment(dateFromBackend, 'yyyy-MM-ddTHH:mm:ss.fffZ').format('MMM DD HH:mm')
Formating options are available here
Upvotes: 0
Reputation: 1688
I would suggest you to use some external library like moment.js https://momentjs.com/
This will do the conversion for you
Upvotes: 0