0

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

brook18
  • 179
  • 2
  • 5
  • 12

1 Answers1

0

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%

Haythem
  • 384
  • 1
  • 12