Page 1 of 1

Date modification

Posted: Wed Nov 10, 2010 6:32 pm
by scott47303
OK, i have a date such as 01-June-2010, and i want to subtract 14 days from it, and I am getting stumped. I tried to convert to dateitems, to change it but I guess it does not work since it doesnt like the input format?

Re: Date modification

Posted: Wed Nov 10, 2010 7:14 pm
by jmburnod
Scott,

This function return the short date one week before
I hope this help

Code: Select all

function OneWeekBefore
   put the date into mydate
   convert mydate to seconds
   subtract (86400*7) from mydate
   convert mydate to short date
   return mydate
end OneWeekBefore
Best

Jean-Marc

Re: Date modification

Posted: Thu Nov 11, 2010 9:04 am
by Janschenkel
You're right, the engine doesn't know how to convert from your input format to its internal format.
Something like this should do the trick:

Code: Select all

on mouseUp
   local tDate
   put ConvertToDateItems("01-June-2010") into tDate
   subtract 14 from item 3 of tDate
   convert tDate from dateItems to dateItems
   answer ConvertFromDateItems(tDate)
end mouseUp

function ConvertToDateItems pDisplayDate
   local tDateItems
   set the itemDelimiter to "-"
   put item 3 of pDisplayDate & "," & \
          lineOffset(item 2 of pDisplayDate, the monthNames) & "," & \
          item 1 of pDisplayDate & ",12,0,0,0" \
          into tDateItems 
   return tDateItems
end ConvertToDateItems

function ConvertFromDateItems pDateItems
   local tDisplayDate
   set the itemDelimiter to ","
   put format("%2u", item 3 of pDateItems) & "-" & \
          line (item 2 of pDateItems) of the monthNames & "-" & \
          format("%4u", item 1 of pDateItems) \
          into tDisplayDate
   return tDisplayDate
end ConvertFromDateItems
Note the line convert tDate from dateItems to dateItems - it looks odd, but it triggers the engine into re-calculating the date items, automatically rectifying sout-of-bounds months, days, hours, minutes and seconds. Also note that I used 12 (noon) as the hour to avoid surprises due to daylight savings time; may not be necessary, but better safe than sorry :-)

HTH,

Jan Schenkel.