WinBatch® Technical Support Forum

Archived Boards => WinBatch Script Exchange => Topic started by: johanjo on August 25, 2013, 01:23:13 PM

Title: date parse and date difference
Post by: johanjo on August 25, 2013, 01:23:13 PM
Hi,

Is there any one can help me or show me a sample code that will parse string date and get the date difference in days.

sample date: 12/22/2012 then I want to get the difference in days. Basically I want to know if the date is older or lesser than 180 days(6 months) old from today's date.

Possible date formats to be parsed 12/22/2012 or 2012 ( full date or just the year but I'm more concern of the full date)


Please help me. Thanks :(
Title: Re: date parse and date difference
Post by: snowsnowsnow on August 26, 2013, 05:48:11 AM
The general method is to convert your format into the WB "standard" YMDHMS format.

Then use any of the TimeDiff functions to calculate the difference.

It's all in the manual...
Title: Re: date parse and date difference
Post by: Deana on August 26, 2013, 09:58:58 AM
In order to calculate the numbers of days you will need to pass at least the year, month and the day. Here is a trick that uses the ObjectType function to attempt convert your date string to a valid YMDHMS format:

Code (winbatch) Select
;Is there any one can help me or show me a sample code that will parse string date and get the date difference in days.
;sample date: 12/22/2012 then I want to get the difference in days. Basically I want to know if the date is older or lesser than 180 days(6 months) old from today's date.
;Possible date formats to be parsed 12/22/2012 or 2012 ( full date or just the year but I'm more concern of the full date)

strDate = "2012:12:22"
strDate = "12/22/2012"
strDate = "12-22-2012"

;Confirm we have a valid date string
; Requires at least a Year Month and Day vlaue
newstring = StrClean(strDate, ":/-", "", @FALSE, 2)
If StrLen(newstring) < 2
   Pause( 'Notice','invalid date')
   Exit
Endif

ymdDate = ObjectType("date", strDate)
ymdToday = TimeYmdHms() ; YYYY:MM:DD:HH:MM:SS
DiffDays = TimeDiffDays( ymdToday, ymdDate )
Message('Difference in Days', DiffDays)

Title: Re: date parse and date difference
Post by: johanjo on August 26, 2013, 12:12:39 PM
Thank you Deana.

This is perfect.. Thank you Thank you .. so cool! :)

I do have another question though. Do you know how to compare dates as to which is older or recent? Can I do this?

Code (winbatch) Select
ymdDate1 = ObjectType("date", "08/27/2013")
ymdDate2 = ObjectType("date", "09/27/2013")

If ymdDate1 < ymdDate2
....
EndIf

Title: Re: date parse and date difference
Post by: DAG_P6 on August 30, 2013, 03:27:44 PM
You can compare a pair of dates without converting them to COM objects. The standard WIL date format is YYYY/MM/DD hh:mm:ss. That being the case, you can compare a pair of them like so.


ymdhmsDate1 = '2013.08.30.16.30.25'
ymdhmsDate2 = '2013.09.09.11.10.14'

if ymdhmsDate1 > ymdhmsDate1  then
    ; Handle Date1 following Date2.
else
    ; Handle Date1 preceding Date2.
endif ; if ymdhmsDate1 > ymdhmsDate1  then


This format can be fed to the TimeDiffDays() and TimeDiffSecs() functions to compute days or seconds, respectively, between dates.