Save data from Datetime in variable

Asked

Viewed 137 times

1

Good morning, I am finishing an Alarm application where the user selects the time the Notification should appear, I am using the native component of Ionic, the ion-Datetime. But I can’t find anywhere how I pass the data that the user selected into a variable. The part of the notifications I have already resolved with the this.hora, this.minuto inside the Trigger.

this.localNotifications.schedule({
            title: 'Atenção',
            text: '',
            data: { mydata: 'Notificação' },
            trigger:{ every: { hour: this.hora , minute: this.minuto}, count: 1 },
            foreground: true

The problem is to pass the time and minute of the Picker component to these variables.

In html it looks like this:

 <ion-datetime [displayFormat]="HH:mm"></ion-datetime> 

What would be a way to spend hour and minute into these variables?

  • What you want is to take the values of datetime is this?

  • That, take the values of the hour and minute (in my case)

  • What RZP means?

1 answer

2


You can take the value of datetime through a [(ngModel)] for example, then you will have the value of the type string in the format that datetime produces, which would be: 10:00, then you would have to separate the string to take the value of the hora (10) and of minutos (00), with a method Slice for example. From there the values of the variables within the Trigger will already be correct:

TS

tempo: string;
hora: string;
minuto: string;

Tempo() {
   this.hora = this.tempo.slice(0,2);
   this.minuto = this.tempo.slice(3,5);
}

this.localNotifications.schedule({
   title: 'Atenção',
   text: '',
   data: { mydata: 'Notificação' },
   trigger:{ every: { hour: this.hora , minute: this.minuto}, count: 1 },
   foreground: true
})

HTML

<ion-datetime displayFormat="HH:mm" [(ngModel)]="tempo" (ngModelChange)="Tempo()">
</ion-datetime>

You can see an example working here.

  • Perfect, really good! Something else, instead of spending hour and minute as a string, I can transform to number? Localnotifications only accepts number for hour and minute

  • 1

    Sure, just use the Javascript method Number(), being like this: Number(this.hora) and Number(this.minuto).

  • Another question, what it contains in the variables this.hora and this.minuto ? Logcat gives me an error that I can’t convert them into numbers. cannot be cast to java.lang.Integer...at...localnotification.LocalNotification.schedule

  • Or would have some way to turn into number(integer) before passing the variables within the Localnotification Schedule?

  • Strange, if you did Number(...) it was already meant to work, you can convert Number into another variable and put this variable already converted in Rigger if you want, has tbm the method parseint(), but, the Number is already to work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.