-1

I have problem to convert the date 19990813 of type string given by the backend to 13.08.1999 with the dots. I am not able to do so..

Using Angulars in-build date-format-function I got always the wrong date.

How can I achieve my goal?

I am thankful for any advice.

Kind regards

Anna Marie

Hien Nguyen
  • 21,001
  • 7
  • 35
  • 48
Anna Marie
  • 91
  • 1
  • 9
  • Do you want it to be a pipe operator? That is preferred if you're using it in a template. – Fredrik_Macrobond May 02 '19 at 07:49
  • 1
    What if i tell you that *the date 19990813 (YYYY-MM-DD)* is actually of format `YYYYmmDD` ?? – Antoniossss May 02 '19 at 07:50
  • If it is preferred, I would like to use it :). I have to search a little bit. Thanks for the advice – Anna Marie May 02 '19 at 07:51
  • yes, @AnnaMarie, for performance reasons you should definitely use the pipe operator. I will post the code for it in a couple of minutes. – Fredrik_Macrobond May 02 '19 at 07:52
  • @Antoniossss Yes, I wanted it to be a little bit more clearly shown, but you are may right :) – Anna Marie May 02 '19 at 07:53
  • But it you think about it, if that is `YYYYmmDD` then `DD.mm.YYYY` is what you want. so `| date :"DD.mm.YYYY"` – Antoniossss May 02 '19 at 07:55
  • @Antoniossss I believe it would be a problem using the default date pipe, had it been `YYYY-MM-DD` then something like this could had worked: `{{"1999-08-13" | date: "dd.MM.yyyy"}}` – Ashish Ranjan May 02 '19 at 07:59
  • 1
    Moment pipe is here but you would need to put `pipes` in `app.module` https://stackoverflow.com/questions/38490410/how-to-implement-moment-js-as-a-pipe-in-an-angular2-project – Andrew Allen May 02 '19 at 08:13
  • Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Liam Jun 19 '19 at 10:33
  • There are literally hundreds of duplicates of this question. Maybe try google next time? – Liam Jun 19 '19 at 10:36

3 Answers3

5

You can install npm i moment and use like this

datestring = '19990813';
  constructor(){
     let date = moment(this.datestring, 'YYYYMMDD');
     let datedot = date.format('DD.MM.YYYY');
     alert(datedot);
  }

Demo https://stackblitz.com/edit/moment-js-format

Hien Nguyen
  • 21,001
  • 7
  • 35
  • 48
  • Similarly moment pipe is here but you would need to put pipes in app.module https://stackoverflow.com/a/38491960/4711754 – Andrew Allen May 02 '19 at 08:14
  • yes, if you need use this format in other file, you should create a custom pipe for it – Hien Nguyen May 02 '19 at 08:15
  • I really can't see the need for a date library to reformat a string that requires a single expression. You can also post the code here as a runnable snippet. – RobG May 02 '19 at 13:13
  • i suggest momentjs because may be they need more format in real project – Hien Nguyen May 02 '19 at 13:16
3

You can use String replace() for this.

let date = `19990813`

let regex = /^(\d{4})(\d{2})(\d{2})$/

let newDate = date.replace(regex, "$3.$2.$1")
console.log(newDate)
Ashish Ranjan
  • 11,263
  • 4
  • 22
  • 45
0

If you need to do this string transformation in a component's template, the best choice (for performance reasons) is to use a pipe operator.

Use the solution proposed by xyz for the string transformation itself.

import {Pipe, PipeTransform} from '@angular/core';

@Pipe({
    name: 'myDateFormat'
})
export class MyDateFormatPipe implements PipeTransform {

    transform(value: string, args?: any): any {
        if (!value) {
            return value;
        } else {
            // process the 'value' param and return its result according to xyz's solution
        }
    }
}

then in your template, use it like this:

inputDate | myDateFormat

where inputDate is '19990813'.

And don't forget to import this class to the angular module that will use it.

Fredrik_Macrobond
  • 1,716
  • 18
  • 27