A good formula for timediff (difference in years and 2 decimal points)

Started by sinus, May 24, 2022, 08:46:09 PM

Previous topic - Next topic

sinus

From the help (https://www.photools.com/help/imatch/var_basics.htm)
timediff   This operation calculates the difference in seconds between the variable value and the specified value.
The variable value and the supplied literal value or variable must be formatted in ISO8601 format: YYYY-MM-DD HH:MM:SS (2019-12-24 12:00:00).


I have a question that shames me a little.
As you can see above, you can calculate time differences with this variable, which is quite interesting.

Now I want to find out how to calculate the time difference between a picture and the current time.

But the time in seconds is of course not so helpful, what I am looking for would be a time in years, months, weeks and days.

But it could also just be a decimal expression, that would also help, such as

74.85 years or 74 years 10 month 2 weeks and 3 days

(finally simply that someone can see quickly the time).

Now I have a formula, but it's handmade by me and certainly quite amateurish.

It is
{File.DateTime|format:YYYY-MM-DD hh:mm:ss;math:timediff,{Application.DateTime|format:YYYY-MM-DD hh:mm:ss};cast:float,02;math:div,31536000}

and still gives me e.g. the result -73

In years, that's about right.

Do any of you with mathematical understanding have a formula for this?
Would be great, by the way, you can test this like almost everything good in VarToy.

Best wishes from Switzerland! :-)
Markus

Mario

Programming languages usually have powerful date & time libraries and for languages like JavaScript libraries like Luxon exist, which contain all the tables and functions needed to do this right. JavaScript also has learned a lot about date and time math in recent years. Still, date and time calculations can be quite difficult. Just think: "leap years".

The timediff operator was added to the math function to make it easy to compare the two timestamps, to tell which one is earlier, equal or later.

Comparing two timestamps in a variable and then outputting different intervals like years or seconds is possible, but would require maybe a day or two of design and development and documentation. Feel free to add a feature request so we can see how many users would need such a variable.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

sinus

Thanks, Mario

for your very fast answer.
It is not that important, to add a feature request.

At least I have number: 31536000
Divide seconds with this gives me finally a rough number of years.

I will google a bit more, maybe I find something better.
And yep, you are correct, leap years comes also in this play, I have not thought at that.  :-\

Finally it is quite interesting, clicking on an image and see "wow, this was 47 years ago"!   :)

All because you created such a coo variable.
Best wishes from Switzerland! :-)
Markus

sinus

ok, final information, just in case someone is interested.  8)

I am not mathematically gifted and know very little about programmed.
But it's always what you need effectively that counts.

I didn't find a solution for the problem with the time difference that I could do anything with.
However, I have concocted a variable that is certainly not very good, but it works in such a way that I am (currently) satisfied.

{Application.DateTime|format:YYYY-MM-DD hh:mm:ss;math:timediff,{File.DateTime|format:YYYY-MM-DD hh:mm:ss};math:div,31557600;numformat:float,05,1;pereplace:0,0==not long ago;pereplace:1,0==1 year;pereplace:,0== years}

I need this variable with D&P and my timeline. So for a picture, it tells how long ago this picture was from today.
With pictures that are longer ago it is not such a problem, whether a picture is 74 years ago or 74.7 is not so important for me, although a correct indication would be nice.

The result of the variable is only in years (and unfortunately without decimals, that didn't work).
So the output is unformatted
0,0
1,0
..
70,0

I did this with pereplace (instead of postfix) so that it doesn't say 0.0 years at the end.
0 years now says not long ago
for 1 year it says 1 year
for several years x years

As I said, this is a variable for poor people, but it is ok for me at the moment, maybe one day I will find a more correct solution.

Best wishes from Switzerland! :-)
Markus

sinus

I now find a better variable, especially more accurate.
The output is the year compared to today.

So you can see how long ago a photo was taken.
I have set the decimal places to 2 (I couldn't figure out how to round correctly), so that the results are displayed like this:

41,26
0,73

With postfix you can add years so that the result looks correct.

The variable:
{Application.DateTime|format:YYYY-MM-DD hh:mm:ss;math:timediff,{File.DateTime|format:YYYY-MM-DD hh:mm:ss};math:div,31557600,002;numformat:float,05,02}

Set it into VarToy, click on an image and the result should be correct, except leap year, but if an image is 23,35 years ago or 23,39 years ... to be honest, does not matter me.  :D



Best wishes from Switzerland! :-)
Markus

Mario

-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

Mario

I've decided to add a new variable function named duration for the next release.
It calculates the time difference / duration between two timestamps and returns the result in the unit requested by the user (years, months, days, hours, minutes, seconds).

For example:

{File.DateTime|format:YYYY:MM:DD hh:mm:ss;duration:now,years,one year,$ years,less than a year}

This variable compares the DateTime of a file with now (current date and time) and then returns:

one year
n years
less than a year

depending on the difference between the two timestamps in years.

The last 3 arguments are optional. If you omit them, the function just returns the number of years as a formatted number, e.g.

0
1
16
22

As usual, you can nest variables. For example, for the case that the distance is less than a year, you can just do a:

{File.DateTime|format:YYYY:MM:DD hh:mm:ss;duration:now,years,one year,$ years,{File.DateTime|format:YYYY:MM:DD hh:mm:ss;duration:now,months,one month,$ months,{File.DateTime|format:YYYY:MM:DD hh:mm:ss;duration:now,days,one day,$ days,less than a day}}}

This neat variable emits the number of years. If the distance is less than a year, it emits the number of months. If the distance is less than a month, it emits the number of days. If the distance is less than a day, it emits "less than a day".


-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

sinus

Wow!
Very good news, thanks for this.
Will be very interesting.  :)
Best wishes from Switzerland! :-)
Markus

sinus

Hi Mario
I hope I'm not annoying you, it's just for your information.
I already had a result, but thanks to your new formula idea (duration) I was able to refine my formula.
I probably wouldn't have thought of that with under a year and under a month....
If I compare it with what I had before, it is much better now.  :)

I tried the new duration, but somehow I couldn't do it with it, but your "timediff" did it again.

What I was looking for was an indication of how long ago a photo was taken:
2.4 years ago
5.5 years ago
10.9 years ago

so 1 decimal place, because it is important for me whether something was 2 years ago or 2.5 years ago.

If it's less than 1 year ago, I find months interesting, because people are more likely to talk about "8 months ago" than "32 weeks ago".
Therefore
7..4 months ago
1.5 months ago

1 decimal place

And then, below a month you probably talk more about weeks or days, I limited that to days, so:
22 days ago
13 days ago
4 days ago

Without decimal place, because I don't think that's so important here.

Means, over 1 year, the result is e.g.
14.9 years ago

under 1 year:
7.5 month ago

under 1 month:
6 days ago

All this is not top-accurate, but it is definitely accurate enough for my purposes.

Just, in case you are interested for my quite long variable", what works very good.

{Application.DateTime|format:YYYY:MM:DD hh:mm:ss;math:timediff,{File.DateTime|format:YYYY:MM:DD hh:mm:ss};math:div,31557600,002;numformat:float,05,01;pereplace:,==.;numcomp:lt,1,{Application.DateTime|format:YYYY:MM:DD hh:mm:ss;math:timediff,{File.DateTime|format:YYYY:MM:DD hh:mm:ss};math:div,2635200,002;numformat:float,05,01;numcomp:lt,1,{Application.DateTime|format:YYYY:MM:DD hh:mm:ss;math:timediff,{File.DateTime|format:YYYY:MM:DD hh:mm:ss};math:div,86400,002;numformat:float,05,0;postfix: Tage her},{Application.DateTime|format:YYYY:MM:DD hh:mm:ss;math:timediff,{File.DateTime|format:YYYY:MM:DD hh:mm:ss};math:div,2635200,002;numformat:float,05,01;postfix: Monate her}},{Application.DateTime|format:YYYY:MM:DD hh:mm:ss;math:timediff,{File.DateTime|format:YYYY:MM:DD hh:mm:ss};math:div,31557600,002;numformat:float,05,01;postfix: Jahre her}}

I am quite happy with this, thanks for the great variables, what you always delivers.
Best wishes from Switzerland! :-)
Markus

Mario

I doubt pretty much that many users would ever need to output something like to see something like 2.3 years ago...
If the new duration function does not do what you want, you will have to roll your own variable. It should work fine for most users who just want to display durations. If not, let me know via a feature request.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

sinus

Quote from: Mario on June 03, 2022, 01:23:09 PM
I doubt pretty much that many users would ever need to output something like to see something like 2.3 years ago...
If the new duration function does not do what you want, you will have to roll your own variable. It should work fine for most users who just want to display durations. If not, let me know via a feature request.

Sorry, for me all is very good.
If I take duration or timediff, makes no difference for me, both are ingenious created by you, so I have the choice and everything is cool.
So we have with IMatch, as so often, several possibilities. It is really simply cool.
Great. You did a superfine job.  :)
Best wishes from Switzerland! :-)
Markus