adding a decimal amount to a date
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
adding a decimal amount to a date
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
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
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
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
Wow, your so smart!
How would you write that?
Thanks,
Patrick
How would you write that?
Thanks,
Patrick
Re: adding a decimal amount to a date
Hi Patrick,
I'm crap at dates but try this:
Simon
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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: adding a decimal amount to a date
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
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
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
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
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.
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
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
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
Yes, dateItems is accurate:
Note that you put the new date into item 2 the months (not item 1).
Why do I make things so difficult
grrrr
Simon
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
Why do I make things so difficult

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: adding a decimal amount to a date
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
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
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.
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.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: adding a decimal amount to a date
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
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
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

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: adding a decimal amount to a date
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"
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: adding a decimal amount to a date
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
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