in practice I want to obtain the days that are missing between the expiration date and today's date.
I am attaching the image where my attempts are shown.
Screen1.Initialize is too early to try to process the result of a Date Picker.
Move that logic to the Date Picker's event block that fires after a date has been picked.
where is the expiration date in your screenshot?
I only can see the current date...
as @Taifun said where is the expiration date ? You saved it in a variable? You need a persistent one to get data after app closed.
Second thing you can't use math block between different kind of data . In you block you want to subtract a date ( 3/30/2023) and a number (system time) .
Also for the block duration , you have to use the same kind of data , in the block info you can see which one you have to use (milliseconds for duration, if I remember correctly)
I followed your advice and now I can calculate the remaining days. Anyway for those who need it; I present the procedure below:
Taifun, through the advice of @Raffaele_Gold, I managed to subtract the expiration date - today's date; but the days are calculated only when I click on the ListPicker and set the expiration date. In fact they are not stolen day by day.. as happens in an app that works in the background; so I'll just have to run alarm manager.
It looks like you are not happy with the solution?
To find the best solution you, the first thing you should do is to describe exactly what you need...
For example you always can store the expiration date in TInyDB and do some calculation during Screen Initialize without clicking a list picker
Whenever I enter the expiration date through the ListPicker and save it in a TinyDB; mi correctly calculates the days that elapse between the due date and today's date; and I obviously reload them when I restart the app.
While if I try to disable the call to TinyDB; and to remain the calculation of the days in initialize; of course it ends up subtracting today's date - today's date
Which value is in label Date when you open screen 1. Your calculation starts before adding value by list picker. When you select a date with list view?
In the second image you disabled also the set label text to...
In the date label is today's date; for this reason the calculation ends up subtracting today's date minus today's date
Ah yes , sorry
You have two ways
- When initialize,before set the calculation you have to get value from list picker and then set the math block.
- You can retrieve expiration data from tinyDb but you have to save it the first time , after saving in tinyDb all the time you start app you will get the expiration date
Mi spiego meglio. Quando parte l'app deve avere già salvato questo dato altrimenti bisogna fornirlo. Hai due possibilità
- Quando carica lo screen prima selezioni la data e poi imposti il calcolo
- La prima volta salvi la data di scadenza nel tinyDb ed a ogni ingresso valuti se c'è il valore nel tinyDb e lo prelevi per il calcolo .
Obviously, by first selecting the expiration date and then the calculation; everything is great. If instead I save the calculation in the TinyDB; even if I run the app the following day; it tells me that the remaining days are the same as the day before, since it can't run in the background.
You have to set the get value from TinyDb as first block. Save it in a variable or set directly in the label and then starts the calculation
The calculation is always performed every time I go to indicate the expiry date; otherwise the same days remain..since it doesn't work in the background. I hope they can integrate into Mit App Inventor one day; the possibility that the apps can also work in the background. @Taifun has already taken a first step in this direction..with his extension "Taifun Alarm Manger".
If you save the expiration day in the tinyDb you will find it when open the screen. The only thing that doesn't work is a notification when app is closed. Background task works only with some extension
If the app is in the background, there is no need to calculate the remaining days... nobody will be able to read it anyway...
Therefore after opening the app again, them do the calculation using the stored expiration date from TinyDB
duration = expiration date - current date
I fully agree with you, if every time I open the app; I have to calculate a different expiration date. Instead the expiry date is always the same (e.g. 20 days left)..so if I open the app after two days; he will have to tell me that there are 18 more days to go.
If you save 30/03/2023 as expiration day when you open the app in 20/03/2023 it will show you 10 days to go. If you open the app in 25/03/2023 it will show 5 days to go. With some extra code in 5/04/2023 you will also show : it's 5 days after expiration day and so on