Comparing a date value with a day string

Just starting out? Need help? Post your questions and find answers here.
User avatar
bamsagla
User
User
Posts: 62
Joined: Sat Jan 30, 2010 10:10 am
Location: Laufen, Bavaria, Germany

Comparing a date value with a day string

Post 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.
- Sherlock Holmes - "When you have eliminated the impossible, whatever remains, however improbable, must be the truth."
In my opinion, he must have been a programmer.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Comparing a date value with a day string

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Re: Comparing a date value with a day string

Post by akj »

Here is a slight simplification:
Replace

Code: Select all

Secondday.i = Date(Year(Datevalue), Month(Datevalue), Day(Datevalue), 0, 0, 0)
with

Code: Select all

Secondday.i = Date()/86400*86400
where 86400 are the number of seconds in a day (24*60*60)
Anthony Jordan
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Comparing a date value with a day string

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
bamsagla
User
User
Posts: 62
Joined: Sat Jan 30, 2010 10:10 am
Location: Laufen, Bavaria, Germany

Re: Comparing a date value with a day string

Post 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... :D

Many thanks again, Harry.
- Sherlock Holmes - "When you have eliminated the impossible, whatever remains, however improbable, must be the truth."
In my opinion, he must have been a programmer.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Comparing a date value with a day string

Post by wilbert »

bamsagla wrote:Now the only problem remains which solution to use within my code... :D
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.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply