Page 1 of 1

Subracting dates

Posted: Wed Oct 15, 2008 11:09 pm
by Glenn Boyce
I have a date input into a field as 14/10/2008 and "the Date" automatically input into another field. I want to subtract one from another and get a result of days (not hours or minutes) overdue etc. I've had a few goes i guess the input date probably needs to be formatted somehow. Also "the date" is input in American format - ie mmddyy and I need it as ddmmyy. Assistance appreciated.

Posted: Thu Oct 16, 2008 5:54 am
by Janschenkel
The 'convert' command and the 'system' keyword are your friends.

With 'the system date' you can get the current date in whatever the system format is.

Code: Select all

  put the system date into field "Today"
Assuming you have two fields 'Date_1' and 'Date_2' you can get the difference as follows.

Code: Select all

on mouseUp
  put field "Date_1" into tDate1
  put field "Date_2" into tDate2
  convert tDate1 from system date to seconds
  convert tDate2 from system date to seconds
  put tDate2 - tDate1 into tDifference
  divide tDifference by 60 * 60 * 24
  answer "There is a difference of" && tDifference && "days."
end mouseUp
And just to give you a hint for a future extension where you want to know what day it is 6 weeks from now.

Code: Select all

on mouseUp
  put 6 * 7 into tNumDays
  put the date into tDate
  convert tDate from date to dateItems
  add tNumDays to item 3 of tDate
  convert tDate from dateItems to system date
  answer tNumDays && "days from now is:" && tDate
end mouseUp
Hope this helped,

Jan Schenkel.

Posted: Thu Oct 16, 2008 9:27 am
by SparkOut
You may also find it helpful to try the "useSystemDate" property.
It is a local property (so it needs to be set for each handler that may handle dates, and will reset itself at the end of every handler in which it is used).
If you

Code: Select all

set the useSystemDate to true
at the top of every handler that caters for dates, then all the dates will be "system" dates and you won't have to change hundreds of lines of code (hopefully).

Posted: Thu Oct 16, 2008 11:21 pm
by Glenn Boyce
Each of these will be very useful. Got it working. Many Thanks