A
A
angelzzz2018-08-18 12:46:13
Angular
angelzzz, 2018-08-18 12:46:13

How to convert date format 3:32 AM - 17 Aug 2018 to Aug 17 2018?

There is a json that gives a date value in the form 3:32 AM - 17 Aug 2018. As far as I understand this is a string. Is it possible to somehow just convert it to Aug 17 2018?

console.log(new Date(value)); - gives Invalid date.

I found nothing easier than converting through a custom filter like this

transform(value: string): string {
        var timeToDate = value.split(/- /),
        date = timeToDate[1].split(/ /),
        day = date[0], 
        month = date[1],
        year = date[2],
        newDate = month + " " + day + ", " + year;
    return newDate;


In html, respectively, I call like this: Maybe there are more elegant and simple solutions? In order not to go twice, how can I access this variable in the ts file of the component ?
<span >{{ line.date | stringToDate }}</span>


line.date

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2018-08-18
@Xuxicheta

There is a json that gives a date value in the form 3:32 AM - 17 Aug 2018. As far as I understand this is a string. Is it possible to somehow just convert it to Aug 17 2018?
console.log(new Date(value)); - gives Invalid date.

Truth? jsfiddle.net/vg9fn20h/2
And for formatting there is a DatePipe
this.line.date

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question