Calculate time (day) difference between two instants?

Hi all,

I'm stuck with something that should be very simple.

  • Get & display current time
  • Save that time
  • Show time gap since last saved.

Eventually, I need somthing like that to calculate how many days have passed since last save, but I figure it's easier to experiment with the seconds intervall, as I would otherwise need to wait for midnight to test.

The error I keep getting is: The operation Duration cannot accept the arguments: , ["java.util.GregorianCalendar[time=1699092517324,areFieldsSet=true,areAllFie...

I have attached screenshots and the app.
Thanks for any help!


TimePassed.aia (2.8 KB)

You cannot save an instant to the tinydb

1 Like

Try this way

You can make use of DateTools 's EpochDifference block to find difference in plain format.

1 Like

Your blocks will cause an error in the UpdateDisplay procedure. Because TinyDb stores milliseconds and not instant.

1 Like

I will test it again but at first look works in companion

Thanks for your reply. I tried this but get errors.

Argument to MakeInstant should have form MM/dd/YYYY hh:mm:ss, or MM/dd/YYYY or hh:mm

This is what I get in companion but you should try @Patryk_F suggestion

Clock1.SystemTime returns the current time in Milliseconds from 1970, a simple number.

Subtract two of those and you get interval in Ms.

Then divide by 1000 to get seconds
Divide by 60 to get minutes

1 Like

Try this:

This works! Thank you very much!
Learnt something new that "TinyDb stores milliseconds and not instant".

Thank you also @AGB for those additional explanations, but what if I want to do this not for seconds but for days?

For my app, I want the app to recognize how many days have passed since the last "save". If the last "save" was a second before midnight and I open the app again 1 second after midnight, I want that to count as one day later.

Thanks all!

Divide the seconds by 86400 to get "days"

But will that work even when less than a day has passed, as in the example I outlined above with the timestamps two seconds apart but on different days?

Basically, I want it to count one day even when just a second has passed, as long as midnight was passed.

In my opinion, it would not be logical. 1 day is 24 hours, so you can't say that something happened one day ago when it really happened 2 minutes ago. You can say that something happened yesterday without knowing exactly when. What will you display when 24 hours and 5 minutes have actually passed? Do you just want to display days without hours or minutes?

Difference by date:


Yes, I want the app to tell me how many days have passed in the way that a person would say something took place yesterday, or one day ago, even though it's just after midnight. In other words, how often has midnight been passed since the last date save event.

I have an example of how to do that with a date picker (see screenshot and app attached), but now I want it to do that when screen 1 is inialized, basically simulating someone pressing the date picker and immediately clicking OK to stick with the default option (which is today's date)

ChangeDateCalculateGapInDays.aia (3.1 KB)

I'll try this, thanks, really appreciate it

You could use TFormat extension by @Anke and calculate the duration in days, minutes, seconds

1 Like

Yes, thanks Ria, might do that if this fails. Just waiting 13 minutes till it's midnight here to see if the blocks Patryk suggested do the trick for me :slight_smile:

If not, I'll just go with the extension, which might be easier for this.

This is what I have now. I hope that equals the example Patryk suggested.

1 Like

I works! Result attached in screenshot. I'll also post the entire blocks here for anyone looking at this in futere

Thanks all!

You could also try this:

TimePassed_revised.aia (4.9 KB)