Get and format Date in component


#1

I have in component home.ts

myDate: String = new Date().toISOString();

I would like to get the values this date and pass to providers, in console I see

  ngOnInit() { 
    console.log(this.myDate);
    output = 2018-05-11T14:28:57.329Z
  }

In my country I want to use 11/05/2018
and my method created I have


  getForm() {
    this.user = this.fb.group({
      name: ['', Validators.required ], 
      ...
      created_at: this.myDate
    })
  }

this.auth.createUser(this.setLawyer(), 'lawyers')
                .then((result) => {
                  this.navCtrl.setRoot('LoginPage')
                  .then(() => {
                    this.showAlert('Sucesso!', `Você foi cadastrado com sucesso`)
                  });
                }) ;

I created a method to format data before send to providers

  setLawyer() {
    let lawyer = {
      name: this.user.value.name,
      date: this.user.value.created_at.date,
      hour: this.user.value.created_at.hour,
      status: 1,
    }
    return lawyer;
  }

#2

Sorry but I don’t understand your question. You want be able to show your date in forma dd/mm/yyyy ?


#3

I’ll go send to provide all right so I want save in my firebase the date and hour for example this variable this.myDate to show 2018-05-11T14:28:57.329Z ok I want to get the value
date = this.myDate(‘dd/mm/YYYY’)
hour = ths.myDate(‘hh:mm’)
so example I know that is wrong
I do not know if I can explain now @JEricaM


#4

Ok I think I get it.
I save my dates in Firebase database using this code

myDate: new Date().getTime()

and then I retrieve the date from my database, and to show it using dd/mm/YYYY format, I write into HTML

{{myDate | date: 'dd/MM/yyyy'}}

And I see the date in che correct form.

Hope it helps you!


#5

Excelente my friend is true now I done very good thanks so much


#6

Glad I could help :slight_smile:
Have a good day