Page 1 of 1

comparing dates

Posted: Thu Feb 11, 2016 12:34 pm
by ittarter

Code: Select all

if "2/9/16" > "2/11/16" then; put "OK"; end if
Results in "OK"

I'm guessing that the front slashes are being read as divide signs.

Besides converting to seconds, is it possible to compare dates in terms of which is later/earlier? Or do I have to convert to seconds and then compare?

Re: comparing dates

Posted: Thu Feb 11, 2016 12:41 pm
by Thierry
ittarter wrote:

Code: Select all

if "2/9/16" > "2/11/16" then; put "OK"; end if
Results in "OK"
Mmm, try this one:

Code: Select all

if  "9/12/1999" > "1/1/2016" then put "Ok"
Thierry

Re: comparing dates

Posted: Thu Feb 11, 2016 1:16 pm
by ittarter
Nope, it still returns "Ok".

Re: comparing dates

Posted: Thu Feb 11, 2016 1:42 pm
by FredBeck
Time and operators is a tricky thing.
A scary video about time & timezones : https://www.youtube.com/watch?v=-5wpm-gesOY

Re: comparing dates

Posted: Thu Feb 11, 2016 2:52 pm
by Thierry
ittarter wrote:Nope, it still returns "Ok".
Sure it returns the wrong info :)

I wanted to show you that's not the right method.

Check date or time comparison in this forum;
I'm sure you'll find something.

Good luck,

Thierry

Re: comparing dates

Posted: Thu Feb 11, 2016 6:44 pm
by jacque
The short answer is yes, convert to seconds. That's the only way to compare them as numbers.

Re: comparing dates

Posted: Thu Feb 11, 2016 6:45 pm
by quailcreek
This might help.

Code: Select all

function numericDate pDate
   set the itemDel to slash
   
   -- get the month and pad to 2 digits if necessary
   put item 1 of pDate into tMonth
   if tMonth < 10 then put "0" before tMonth
   
   -- get the day and pad to 2 digits if necessary
   put item 2 of pDate into tDay
   if tDay < 10 then put "0" before tDay
   
   -- the year is always 4 digits in the dateItems so just put it together and return the number
   return item 3 of pDate & tMonth & tDay
end numericDate

put  numericDate(fld "Date Start") into tStart
put  numericDate(fld "Date End") into tEnd

if tStart < tEnd then
-- do stuff
end if

Re: comparing dates

Posted: Thu Feb 11, 2016 7:16 pm
by FourthWorld
This won't help in the specific case you're exploring, but helpful for other algos: dates can be compared in the sort command when specifying datetime, e.g.:
sort lines of tList datetime

Re: comparing dates

Posted: Mon Feb 15, 2016 12:07 pm
by ittarter
Thanks for the responses, everyone! I'll convert to seconds to compare, and it's helpful to know that I can sort dates, too :)