Page 1 of 2

adding a decimal amount to a date

Posted: Sun Mar 03, 2013 9:42 pm
by herbwords
Hi,

I wrote this and it works fine with whole numbers. When I put 1.5 into the expire time (fld "setDateD") it won't deal with the .5 which represents 1/2 year. Any suggestions?

on gramExp
put the date into myDate
convert myDate to dateItems
add fld "setDateD" of cd "inventory1" of stack "inventory" to item 1 of myDate
convert myDate to short date
put myDate into fld "exDate"
end gramExp

Thanks,

Patrick

Re: adding a decimal amount to a date

Posted: Sun Mar 03, 2013 10:14 pm
by bn
Hi Patrick,

you could add 6 to item 2 of the dateItems, i.e. months

You add whatever you want but only as integers. The rest you would have to add to the respective items:
year, month, day, hour, minute, second. You can add or subtract any amount from those items. E.g. add 5000 to item 5, = minute and it will convert correctly. Or subtract 18 from month etc.

Kind regards
Bernd

Re: adding a decimal amount to a date

Posted: Mon Mar 04, 2013 5:21 am
by herbwords
Wow, your so smart!

How would you write that?

Thanks,

Patrick

Re: adding a decimal amount to a date

Posted: Mon Mar 04, 2013 6:13 am
by Simon
Hi Patrick,
I'm crap at dates but try this:

Code: Select all

on gramExp
put the date into myDate
convert myDate to seconds
add (fld "setDateD" of cd "inventory1" of stack "inventory" * 31536000)  to  myDate 
--31536000 = number of seconds in a year
convert myDate to short date
put myDate into fld "exDate"
end gramExp
Simon

Re: adding a decimal amount to a date

Posted: Mon Mar 04, 2013 10:14 am
by bn
Hi Patrick,

as Simon says you could do it with seconds. This will give you a good approximation of the date. It all depends how accurate you want your date. Suppose you have a leap year and you may be off. Good chances are that the hour is also off since you might run into daylight saving time problems.
Dateitems take care of all that.

How do you arrive at 1.5 years?
It is hard to give you advice if there is no context to the question. Too much guessing.

Kind regards
Bernd

Re: adding a decimal amount to a date

Posted: Mon Mar 04, 2013 6:18 pm
by herbwords
Thanks Simon.

I tried it and it's close. Using todays date 3/4/13, I set the preferences to 1.5 years (fld "setDateD") it returned 9/2/14, about 1 month off. Maybe the seconds need to be tweeked slightly?

The reason for 1.5 is the format that I want used in the preferences for expiration dates (1.5 years). I could say 18 months but most people don't think in months when your dealing with years most of the time.

Thanks,

Patrick

on gramExp
put the date into myDate
convert myDate to seconds
add (fld "setDateD" of cd "inventory1" of stack "inventory" * 31536000)  to  myDate
--31536000 = number of seconds in a year
convert myDate to short date
put myDate into fld "exDate"
end gramExp

Re: adding a decimal amount to a date

Posted: Mon Mar 04, 2013 6:58 pm
by herbwords
It does work, I was wrong about it being a month off, it looks like it's about 2 days off. But, as stated months are different 30 or 31 days and leap years gain a day. This accuracy is good enough for raw herbs!

It would be interesting to see how accurate it could be written. I'm sure that would be a project!

Thanks to both of you.

Re: adding a decimal amount to a date

Posted: Mon Mar 04, 2013 11:25 pm
by jsburnett
Herbworks,

If you are using a decimal number that the product in years results in an integer, then you could use dateItems.

i.e.,
put 1.5 * 12 into numMos
put the date into vDate
convert vDate to dateItems
add numMosto item 2 of vDate
convert vDate to shortDate

The problem is if numMos is not an integer it throws off the convert to shortDate

John

Re: adding a decimal amount to a date

Posted: Mon Mar 04, 2013 11:58 pm
by Simon
Yes, dateItems is accurate:

Code: Select all

on gramExp
put the date into myDate
convert myDate to dateItems
add (fld "setDateD" of cd "inventory1" of stack "inventory" * 12) to item 2 of myDate
convert myDate to short date
put myDate into fld "exDate"
end gramExp
Note that you put the new date into item 2 the months (not item 1).

Why do I make things so difficult :x grrrr

Simon

Re: adding a decimal amount to a date

Posted: Tue Mar 05, 2013 12:33 am
by herbwords
very cool, thanks John!

your's is cool too simon, i'm sure in perspective it will open other doors for my imagination!

Cheers,

Patrick

Re: adding a decimal amount to a date

Posted: Tue Mar 05, 2013 12:54 am
by mwieder
Patrick-

Note that in Simon's code, if the month value overflows (add 6 months to August and you're in the following year), the "convert myDate to short date" line automatically adjusts the month and year fields so it comes out right.

Re: adding a decimal amount to a date

Posted: Tue Mar 05, 2013 1:14 am
by herbwords
I ran into a problem with John's code.

I put in 1.75 or ( 1 year 9 months) and it won't work with the 5. It only covers 1 decimal place.

So, back to Simon's code!

Thanks all

Re: adding a decimal amount to a date

Posted: Tue Mar 05, 2013 1:39 am
by Simon
Now I'm confused :?
Using dateItems is more accurate then convert to seconds...
The second set of code that I posted uses dateItems just like John's code. Putting 1.75 in gave me 12/4/14, 1.5 results 9/4/14, 1.25 results 6/4/14, 0.5 = 9/4/13 etc.

Oh well.

Simon

Re: adding a decimal amount to a date

Posted: Tue Mar 05, 2013 2:02 am
by mwieder
Truncate the addend to an integer before adding/converting.

Code: Select all

    put the date into myDate
    convert myDate to dateItems
    add trunc(fld "setDateD" of cd "inventory1" of stack "inventory" * 12) to item 2 of myDate
    convert myDate to short date
    put myDate into field "exDate"

Re: adding a decimal amount to a date

Posted: Tue Mar 05, 2013 3:13 am
by herbwords
I'll try your new code Simon.

The problem with John's is off amounts of time like 0.333 for 4 months. It won't work. 1.25 or 1.5 or 1.75 does work. Maybe this should be done as months?

Thanks,

Patrick