date parse and date difference

Started by johanjo, August 25, 2013, 01:23:13 PM

Previous topic - Next topic

johanjo

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 :(

snowsnowsnow

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...

Deana

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)

Deana F.
Technical Support
Wilson WindowWare Inc.

johanjo

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


DAG_P6

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.
David A. Gray
You are more important than any technology.