Compare date

Started by Orionbelt, August 23, 2013, 09:10:01 AM

Previous topic - Next topic

Orionbelt

Hi,
I am trying to compare current today's date with Given date. If today's date is equal or older than given date, exit program. i am having issue with comparing...

strTitle = "Expiration"

strExpireDate            = "08/21/2014"
Today=TimeYmdHms ( )
Y = ItemExtract (1,Today,":")
M = ItemExtract (2,Today,":")
D = ItemExtract (3,Today,":")
Date = "%Y%%M%%D%"                       ; My preferred boiled down date format

;strExpireDate
EY = ItemExtract (1,strExpireDate,"/")
EM = ItemExtract (2,strExpireDate,"/")
ED = ItemExtract (3,strExpireDate,"/")
GivenDate = "%EY%%EM%%ED%" 

IF Date>=GivenDate
      Message(strTitle, " This Programs has expired on :%strExpireDate%")
Exit
Else
            
EndIf

Deana

WinBatch functions return dates in the YMDHMS format: YYYY:MM:DD:HH:MM:SS. Dates that are formatted this way can easily be compared using the < and > operators. As you have done. However your code is improperly extracting out the items from strExpireDate. strExpireDate  is formatted as mm/dd/yyyy. Item 1 is month, item 2 is day and item 3is year. If you modify your ItemExtract statements for the strExpireDate, it should resolve your problem.

For Example:

Code (winbatch) Select
strTitle = "Expiration"

strExpireDate            = "08/21/2014"
Today=TimeYmdHms ( )
Y = ItemExtract (1,Today,":")
M = ItemExtract (2,Today,":")
D = ItemExtract (3,Today,":")
Date = "%Y%%M%%D%"                       ; My preferred boiled down date format

;strExpireDate
EY = ItemExtract (3,strExpireDate,"/") ; Item 3 = YEAR
EM = ItemExtract (1,strExpireDate,"/") ; Item 1 = MONTH
ED = ItemExtract (2,strExpireDate,"/") ; Item 2 = DAY
GivenDate = "%EY%%EM%%ED%" 

IF Date>=GivenDate
      Message(strTitle, " This Programs has expired on :%strExpireDate%")
Else
     Message(strTitle, " This Programs has NOT expired")         
EndIf
Exit
Deana F.
Technical Support
Wilson WindowWare Inc.

Orionbelt

Thx Deana for your quick reply,
how about this
strTitle = "Expiration"
strExpireDate            = "2014:07:21"
Today=TimeYmdHms ( )

IF Today>=strExpireDate       
Message(strTitle, " This Programs has expired on :%strExpireDate%")
Else     
Message(strTitle, " This Programs has NOT expired")         
EndIf

td

Or this works with multiple date formats version.
Code (winbatch) Select

strTitle = "Expiration"
strExpireDate = "08/21/2014" ; Could be YYYY:MM:DD or other date formats.
ymdExpireDate = ObjectType("date", strExpireDate)
ymdToday = TimeYmdHms()

If ymdToday >= ymdExpireDate
   Message(strTitle, " This Programs has expired on :%strExpireDate%")
   Exit
Else
           
EndIf
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Deana

Quote from: usarif on August 23, 2013, 09:47:33 AM
Thx Deana for your quick reply,
how about this
strTitle = "Expiration"
strExpireDate            = "2014:07:21"
Today=TimeYmdHms ( )

IF Today>=strExpireDate       
Message(strTitle, " This Programs has expired on :%strExpireDate%")
Else     
Message(strTitle, " This Programs has NOT expired")         
EndIf

No. You must compare similarly formatted strings...See Tonyd's post for a nice trick for comparing different date time formatted strings.

Created a tech article with this code trick: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/Time~-~Timer~and~Date~Functions/Samples~from~Users+Compare~Different~Date~Format~Trick.txt
Deana F.
Technical Support
Wilson WindowWare Inc.

Orionbelt

thx tony, works perfectly...