Any handy utility function available to calculate number of days between two dates

This is usually something you search online and something pops up, but no such luck in finding something in Squirrel code so thought to pop the question in the forum before embarking on developing something from scratch / hacking something together.

Has anyone done it before and/or willing to share and/or provide helpful tips on creating a utility function that will give me the number of days between two calendar dates. I need this to work out a wakeup timer which could be anything from 1 day up to 248 days. I have it in my code that the user can enter a future date through an html form and this is compared to current date. Just wouldn’t want to be a day late / early due to leap years, miscalculating days in a calendar month etc.

It appears that using the forum search option to search through historical forum posts is rather hit and miss in pulling out relevant content. Having spent more time on it I stumbled across two useful posts… here is one of them as now I cannot find the other one again?? … working on a different computer and cannot quite recall exact search phrases… oh well

For those interested I see @smittytone had posted some useful calendar functions (Oct 14):

Then after a little more thought I determined that the squirrel function to use is time() rather than date() as this gives UTC time since 1 Jan 1970.

And then on my html page I am able to convert a user entered date (in dd/mm/yyyy format) using a javascript function linked to the input box, by creating new date object

and then converting that date to UTC time since 1 Jan 1970 using getTime() function on that new date object and then converting it to seconds (* 0.001)

Then I am able to subtract one from the other once I send my javascript calculated value back to Imp agent… then able to make adjustments based on current time of day etc if needs be… now that proved a little easier once I worked out how :slight_smile:

Since the Imp already uses the cloud, and requires the internet anyhow, I would create a simple PHP API on my own website to do the calculation with PHP, which is easy. The imp agent would query my script with 2 dates and the script would return the answer. PHP can also adjust to any timezone easily. I realize that doing this requires another website (outside of the cloud), but I use my website for many other Imp related things already … such as saving data in a MySQLi database. Or even turning on my Christmas lights only when the word “snow” appears in the current weather conditions on Weather Underground.

Hmmm … my website and my Imp Agent are always discussing things with each other, behind my back. I think it’s actually one of the coolest things about my Imps.

Yes using php would be handy all right and was considering that approach.

Actually I think what is letting me down in terms of flexibility is not being able to do in Squirrel what I can do in Javascript which is create a time value based on a user defined date.

I think you can do this with date(integer) method as the table returned from date function has a unix timestamp bolted on the end. Just a pity you have to be a math genius to first calculate this integer value for your chosen date. If we were able to enter optional parameters for year, month, day etc. as we can in jscript and php, life would be so much easier.

I’m like you. I am very new to Squirrel and don’t have the experience to solve something like that without lots of research and script examples. I am in awe with the Squirrel scripting I see others do … they make it look so simple. Maybe someday I’ll be “worthy” … [sigh]

Will this help solve your problem?

I wrote it a while back when creating the timer() class but it stands alone as well.