Date modification

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
scott47303
Posts: 17
Joined: Tue Aug 10, 2010 3:16 pm

Date modification

Post by scott47303 » Wed Nov 10, 2010 6:32 pm

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?

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Date modification

Post by jmburnod » Wed Nov 10, 2010 7:14 pm

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
https://alternatic.ch

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Re: Date modification

Post by Janschenkel » Thu Nov 11, 2010 9:04 am

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.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply