Page 1 of 1
Comparing a date value with a day string
Posted: Wed Aug 21, 2013 10:46 am
by bamsagla
Hello everyone,
maybe I'm just stupid, but I'm not able to find an easier way to compare a "day string" with a date value to see if they match. For example if a "day string" was yesterday, on a specific day last month or something.
The sample code should explain better. The "day string" is provided on the first line.
Code: Select all
Stringday.s = "2013.04.17"
Datevalue.i = Date()
Firstday.i = ParseDate("%yyyy.%mm.%dd", Stringday)
Secondday.i = Date(Year(Datevalue), Month(Datevalue), Day(Datevalue), 0, 0, 0)
If Firstday = Secondday
Debug "Stringday is today!"
Else
Debug "Stringday is not today!"
EndIf
Thanks in advance, Harry.
Re: Comparing a date value with a day string
Posted: Wed Aug 21, 2013 11:06 am
by wilbert
You can strip the time part of a date value by first dividing it by 86400 and after that multiply it with 86400.
Code: Select all
Stringday.s = "2013.08.21"
Datevalue.i = Date() / 86400
Datevalue * 86400
Firstday.i = ParseDate("%yyyy.%mm.%dd", Stringday)
If Firstday = DateValue
Debug "Stringday is today!"
Else
Debug "Stringday is not today!"
EndIf
Another option is to divide everything by 86400 before you compare.
Code: Select all
Stringday.s = "2013.08.21"
Datevalue.i = Date()
Firstday.i = ParseDate("%yyyy.%mm.%dd", Stringday)
If Firstday / 86400 = DateValue / 86400
Debug "Stringday is today!"
Else
Debug "Stringday is not today!"
EndIf
Re: Comparing a date value with a day string
Posted: Wed Aug 21, 2013 11:07 am
by akj
Here is a slight simplification:
Replace
Code: Select all
Secondday.i = Date(Year(Datevalue), Month(Datevalue), Day(Datevalue), 0, 0, 0)
with
where 86400 are the number of seconds in a day (24*60*60)
Re: Comparing a date value with a day string
Posted: Wed Aug 21, 2013 11:19 am
by wilbert
You could also write a small procedure to get the difference in days
Code: Select all
Procedure DayDifference(date1, date2)
ProcedureReturn date1 / 86400 - date2 / 86400
EndProcedure
Stringday.s = "2013.08.20"
Firstday.i = ParseDate("%yyyy.%mm.%dd", Stringday)
Select DayDifference(Date(), Firstday)
Case 0:
Debug "Stringday is today!"
Case 1:
Debug "Stringday was yesterday!"
Case -1:
Debug "Stringday is tomorrow!"
Default:
Debug "Stringday is not today!"
EndSelect
Re: Comparing a date value with a day string
Posted: Wed Aug 21, 2013 12:13 pm
by bamsagla
Hi wilbert and akj,
thanks a lot for your suggestions. Suddenly I had the idea for a string-type comparsion.
Should also do the job.
Code: Select all
Stringday.s = "2013.04.17"
Datevalue.i = Date()
Comparison.s = FormatDate("%yyyy.%mm.%dd", Datevalue)
If Stringday = Comparison
Debug "Stringday is today!"
Else
Debug "Stringday is not today!"
EndIf
Now the only problem remains which solution to use within my code...
Many thanks again, Harry.
Re: Comparing a date value with a day string
Posted: Wed Aug 21, 2013 12:55 pm
by wilbert
bamsagla wrote:Now the only problem remains which solution to use within my code...

It depends on what you need it for. If it is to check a user input, it doesn't make a difference since it's a single call.
I you need to do a lot of comparisons, a numeric comparison might be better since it's much faster compared to a string comparison.