0


I am using the MatDatePicker for the Selecting date and showing the date in the edit mode of the application.

Now My API is returning me the date in following format which is not working in Edit mode.

"1998-02-02T18:30:00.000+0000"

How can we convert this into Mon Apr 13 2020 18:44:29 GMT+0530

Taufik Pirjade
  • 380
  • 6
  • 26
  • Are you saying you want to display `Mon Apr 13 2020 18:44:29 GMT+0530` as the input text of the Material DatePicker when you initialize it with the value `1998-02-02T18:30:00.000+0000`? Please provide an [mcve] with a clear description if this is not what you mean. – Igor Apr 13 '20 at 13:33
  • @Igor No, if I convert the received date from API to mentioned format which is "Mon Apr 13 2020 18:44:29 GMT+0530" the mat date picker will show the date. – Taufik Pirjade Apr 13 '20 at 13:37
  • It still sounds like you to *display* that to the end user. The actual data should always be handled/persisted as either some type of Date *type* or in ISO8601 notation if it is being communicated as a string. To *display* this format in the UI you need to specify the [`MAT_DATE_FORMATS`](https://material.angular.io/components/datepicker/overview#choosing-a-date-implementation-and-date-format-settings). – Igor Apr 13 '20 at 13:43

1 Answers1

1

use this. U have isos date string u need to convert date and take as string

 toDateString(param){
      return new Date(param).toString()
 }
 // for dd/mm/yyyy
 toDateFormat(param){
     var date1 = param.split('/')
     return  new Date(date1[1] + '/' +date1[0] +'/' +date1[2]).toString();
}

console.log(this.toDateString("1998-02-02T18:30:00.000+0000"))

for javascript demo to see result

  function toDateString(param){
        return new Date(param).toString()
    }
    
    console.log(toDateString("1998-02-02T18:30:00.000+0000"))
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54