viewpoint-particle

Author Topic: date parse and date difference  (Read 2894 times)

johanjo

  • Newbie
  • *
  • Posts: 6
date parse and date difference
« 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 :(

snowsnowsnow

  • Full Member
  • ***
  • Posts: 233
Re: date parse and date difference
« Reply #1 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...

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: date parse and date difference
« Reply #2 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
;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

  • Newbie
  • *
  • Posts: 6
Re: date parse and date difference
« Reply #3 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
ymdDate1 = ObjectType("date", "08/27/2013")
ymdDate2 = ObjectType("date", "09/27/2013")

If ymdDate1 < ymdDate2
 ....
EndIf

DAG_P6

  • Full Member
  • ***
  • Posts: 184
  • WinBatch rocks, and so does Wilson WindowWare!
    • WizardWrx
Re: date parse and date difference
« Reply #4 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.

Code: [Select]
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.