Antonio
Antonio

Reputation: 35

Formatting date object in an Array with moment is giving an unexpected result

I am getting the note data from the MongoDB database and Nodejs server base api, and i am trying to format the dates using moment, below is what the notes arrays look like, and also below the note Array is what i have tried in other to achieve this

notes = [{
date: "2020-02-27T19:35:52.000Z"
text: "alguma coisa"
_id: "1"

date: "2020-02-27T20:20:27.000Z"
text: "o que"
_id: "2"


date: "2020-02-27T20:21:00.000Z"
text: "o"
_id: "3"
}]


getFunction(){
this.api.get("/notes/", _id).subscribe(
      data => {
        let response = data as any;
        let ret = JSON.parse(response._body);

        this.notesData = ret;
        let retNow = this.notesData.map(o => { 
          Object.keys(o).forEach(k => {
            let d = moment(new Date(o[k]))
            if (d.isValid()) o[k] = moment().format("dddd DD-MMM-YYYY HH:mm");
          })
        })
        console.log("Note" retNow)
      }, error => {
        console.log("error", error);
      }
    );}

my HTML

{{note.date}}

The date was formatted right, but the problem is, all the dates are the same, and they are all current date(today's date) please what am i doing wrong here,

Upvotes: 0

Views: 229

Answers (1)

Vishnu8429
Vishnu8429

Reputation: 104

Can you try this

moment(YOUR_DATE).format(DATE_FORMAT);
moment('2020-02-27T20:21:00.000Z').format("DD-MMM-YYYY HH:mm");

Result: 28-Feb-2020 01:51

Upvotes: 2

Related Questions