'null' result return after convert date into this 'yyyy-MM-dd' format

Issue

am follow the guide from How to convert date into this 'yyyy-MM-dd' format in angular 2 and my return result = ‘null’ . can pls advise where is my mistake?

mydate: string ;
date: Date;

postObj={
    unitno:'',
    trackno:'',
    date: new Date(); 
};

addparcel() {
  
   this.mydate = this.datepipe.transform(this.date, 'yyyy-MM-dd');
    
    console.log(this.postObj.unitno)
    console.log(this.postObj.trackno)
    console.log(this.mydate); 
}

enter image description here
enter image description here

Solution

I recommend checking out the angular documentation for datepipe here:

https://angular.io/api/common/DatePipe

TL;DR

this.mydate = this.datepipe.transform(this.date, 'yyyy-MM-dd');

is wrong, it should be something like :

this.mydate = this.datepipe.transform(this.date, 'medium');

Now in your case I assume you want to convert it to Mysql timestamp, to do that I did this:

 this.dte = new Date();
  let aux = this.dte.getFullYear() + "-" + ("0" + (this.dte.getMonth() + 1)).slice(-2) + "-" + ("0" + this.dte.getDate()).slice(-2) + " " + this.dte.getHours() + ":" + this.dte.getMinutes() + ":" + this.dte.getSeconds();

I know it looks long, but it works 100%

Answered By – Haythem

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published