4 Replies Latest reply on May 10, 2010 10:04 AM by TherionWare

# (Date Math) data from text member 1 minus text from member 2

Help!  I'm building something like an "age verifier"

>> I'm looking to subtract dates

I have a text member called "DateToday" -- is todays date

and a text member called "DateYouTyped" --is keyed by the user (first YEAR then MONTH then DAY) using YYYYMMDD format

How do you "pull" the text from the members and use them as part of the formula ?

And what would the formula look like

I used Director help and found

myBirthday = date(19650712)

YourBirthday = date(19450529)

would you do something like

date1 = (member "DateToday").text

date 2= (member("DateYouTyped).text

and then

put date1 - date2  ???  I want the result to go into a text member lets call it "finalanswer"

• ###### 1. Re: (Date Math) data from text member 1 minus text from member 2

The function below should enable you to do what you want.

It returns the specified number of time periods between 1/1/[baseyear> (parameter b) and the specified date (parameter d). Parameter r specifies the return time period type.

This is off the top of my head, but should be easily extensible to do what you want.

Generally speaking dates are complicated, not least because of the different representations people use. There are several xtras that do this kind of thing,

eg: http://www.scirius.com/eng/index.html and it's free unless you need recondite stuff, but doesn't work in shockwave.

Rgds

TW.

--------------

on DatePrd b, d, r

-- this function returns the specified number of time periods between the 1/1/ [base year] and the

-- param b the base year

-- param d this is the target date expected format YYYYMMDD HHMMSS

-- param r 1 = return seconds, 2 = return minutes, 3 = return hours, 4 = return days, 5 = return weeks

-- between 1/1/ base year and the target date

if  (float( the systemdate.year ) / 4.0 = 0) AND (float( the systemdate.year ) / 400.0 <> 0) then

tMon = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

else

tMon = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

end if

-- Assumes d in format YYYYMMDD HHMMSS

dd = integer(chars(d, 7, 8 ))

mm = integer(chars(d, 5, 6 ))

yy = integer(chars(d, 1, 4 )) - b

-- put dd, mm, yy

tt = 0

repeat with x = 1 to mm - 1

tt = tt + tMon[x]

end repeat

repeat with x = 1 to yy - 1

if (float( x ) / 4.0 = 0) AND ( float( x ) / 400.0 <> 0) then

tt = tt + 366

else

tt = tt + 365

end if

end repeat

hh = value( chars(the long time, 1, 2) )

mm = value( chars(the long time, 4, 5) )

ss = value( chars(the long time, 7, 8) )

case r of

1 : tt = ((tt + dd) * 24 * 60 * 60 ) + (hh * 3600) + (mm * 60) + ss -- seconds

2 : tt = ((tt + dd) * 24 * 60 ) + (hh * 60) + mm -- minutes

3 : tt = ((tt + dd) * 24) + hh -- hours

4 : tt = tt + dd -- days

5 : tt = (tt + dd) / 7 -- weeks

otherwise

tt = 0

end case

return tt

end

• ###### 2. Re: (Date Math) data from text member 1 minus text from member 2

Opps. How embarsassing...

That should of course have (probably) read:

on DateSecs b, d, r

-- param b the base year

-- param d this is the target date expected format YYYYMMDD HHMMSS

-- param r 1 = return seconds, 2 = return minutes, 3 = return hours, 4 = return days, 5 = return weeks

-- between 1/1/ base year and the target date

if the number of chars in d = 8 then

d = d && "000000"

end if

-- a leap year is defined as any year that is exactly divisible by 4 but not exactly divisible by 400

-- as in so far as I recall...

if (float( chars(d,1, 4) ) / 4.0 = 0) AND (float( chars(d,1, 4) / 400.0) <> 0) then -- leap year check.

tMon = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] -- is leap year

else

tMon = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] -- is not leap year

end if

-- Assumes d in format YYYYMMDD HHMMSS

dd = integer(chars(d, 7, 8 ))

mm = integer(chars(d, 5, 6 ))

yy = integer(chars(d, 1, 4 )) - b

-- put dd, mm, yy

tt = 0

repeat with x = 1 to mm - 1

tt = tt + tMon[x]

end repeat

repeat with x = 1 to yy - 1

if (float( x ) / 4.0 = 0) AND ( float( x ) / 400.0 <> 0) then

tt = tt + 366

else

tt = tt + 365

end if

end repeat

-- 19800101 235904

-- 123456789012345

if the number of chars in d = 15 then

hh = value( chars(the long time, 1, 2) )

mm = value( chars(the long time, 4, 5) )

ss = value( chars(the long time, 7, 8) )

else

hh = 0

mm = 0

ss = 0

end if

case r of

1 tt = ((tt + dd) * 24 * 60 * 60 ) + (hh * 3600) + (mm * 60) + ss -- seconds

2 tt = ((tt + dd) * 24 * 60 ) + (hh * 60) + mm -- minutes

3 tt = ((tt + dd) * 24) + hh -- hours

4 tt = tt + dd -- days

5 tt = (tt + dd) / 7 -- weeks

6 tt = 0 -- months are difficult to do properly.

7 tt = 0 -- years are less difficult but ...

otherwise

tt = 0

end case

return tt

end

• ###### 3. Re: (Date Math) data from text member 1 minus text from member 2

You had one response that, admittedly, I didn't spend too long picking apart - mostly because it seemed over-complicated.

I would approach what you describe by firstly ensuring there are enough numbers entered to create a valid date - this includes leading zeros in the case of day and month. Either that, or supply separate entry fields for year, month and day that can be individually validated. However, going with your posited 8-digit entry the "trick" is to grab the first 4 characters as the year, the next 2 as the month and the last 2 as the day and coerce them from strings to integers. Something like:

```sDateToday = member("DateToday").text
tYear = value(sDateToday.char[1..4])
tMonth = value(sDateToday.char[5..6])
tDay = value(sDateToday.char[7..8])
dToday = date(tYear, tMonth, tDay)

sDateEntered = member("DateYouTyped").text
tYear = value(sDateEntered.char[1..4])
tMonth = value(sDateEntered.char[5..6])
tDay = value(sDateEntered.char[7..8])
dEntered = date(tYear, tMonth, tDay)

-- this gives the number of days between the 2 dates
-- it's not clear how you want the result formatted, so ...
nDifference = dToday - dEntered
member("finalanswer").text = "There are" && string(nDifference) && "days between the two dates entered"
```
• ###### 4. Re: (Date Math) data from text member 1 minus text from member 2

Erm, Good Point, as trivially shown by:

d1 = date(1980, 2, 29)

d2 = date(1981, 2, 28)

put d2 - d1

-- 365

d1 = date(2000, 2, 29)
d2 = date(2001, 2, 27)

put d2 - d1

-- 365

That said:

d1 = date(2000, 2, 35)

d2 = date(2001, 2, 247)

put d2 - d1

-- 578

Which I suppose just shows the need for input validation!

Nice one.

Best Regards

TW.