Date() in If statement

Post your coding questions such as 'how to' here.
Post Reply
CoolIce

Date() in If statement

Post by CoolIce »

Hi

I was just wondering why

Code: Select all

If Date() = specifiedDate$
doesn't work. Is there another method of doing this?
I want to launch some code when the specifiedDate$ is equal to the day the exe is started and
launch some other code if this is not the case.

I tried

Code: Select all

    If Date() = specifiedDate$
        hooray()
    Else
        loose()
    EndIf
but it always launches the second subroutine (else statement).
User avatar
Misthema
Advanced Member
Posts: 312
Joined: Mon Aug 27, 2007 8:32 pm
Location: Turku, Finland
Contact:

Re: Date() in If statement

Post by Misthema »

CoolIce wrote:I tried

Code: Select all

    If Date() = specifiedDate$
        hooray()
    Else
        loose()
    EndIf
but it always launches the second subroutine (else statement).
It _ALWAYS_ runs the Else -statement, it the If -statement isn't true.

And have you even tried "today" -day (stupidly said :D) on it?
When I tried it, it worked:

Code: Select all

If Date() = "16 Jan 2008" Then Print "Today is 16th day of January 2008"
WaitKey
Remember to input the day like I did on that code.
CoolIce

Re: Date() in If statement

Post by CoolIce »

You don't understand.

Of course I know that specifiedDate$ has to be the "today" day.
I put the date (today) in afile, read it into the variable specifiedDate$ and just want to check with the if-statement if it is really the todays date.

The reading loop works, I tried it via "print".
When I tried it, it worked:
Yes, but you didn't do the same thing. I want to compare the variable to "Date()", not a text. This is a difference, and THIS didn't work in my code.
User avatar
Misthema
Advanced Member
Posts: 312
Joined: Mon Aug 27, 2007 8:32 pm
Location: Turku, Finland
Contact:

Re: Date() in If statement

Post by Misthema »

CoolIce wrote:I want to compare the variable to "Date()", not a text. This is a difference, and THIS didn't work in my code.
Hmm.. Why don't you try to check it this way (if I understanded correctly):

Code: Select all

If specificDate$ = Date() Then
    code...
Else
    more_code...
EndIf
And there must be text in the variable, like "16 Jan 2008" etc., 'coz Date() shows it like that.
CoolIce

Re: Date() in If statement

Post by CoolIce »

that's what i first tried and it didn't work either.

the variable content is like you wrote. so that's not the problem.
TheFish
Developer
Developer
Posts: 477
Joined: Mon Aug 27, 2007 9:28 pm
Location: Joensuu

Re: Date() in If statement

Post by TheFish »

CoolIce wrote:that's what i first tried and it didn't work either.

the variable content is like you wrote. so that's not the problem.
Did you write "jan" or "Jan" in the variable? because this works well for me:

Code: Select all

specifiedDate$ = "16 Jan 2008"
If Date() = specifiedDate Then
    Print "If-statement"
Else
    Print "Else-statement"
EndIf
WaitKey 
Or did you declare specifiedDate in another function than the one youre comparing it in? If you did, it must be a global variable.
CoolBasic henkilökuntaa
Kehittäjä
Post Reply