adding a decimal amount to a date

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

herbwords
Posts: 70
Joined: Sat Dec 01, 2007 2:59 am

adding a decimal amount to a date

Post by herbwords » Sun Mar 03, 2013 9:42 pm

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: adding a decimal amount to a date

Post by bn » Sun Mar 03, 2013 10:14 pm

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

herbwords
Posts: 70
Joined: Sat Dec 01, 2007 2:59 am

Re: adding a decimal amount to a date

Post by herbwords » Mon Mar 04, 2013 5:21 am

Wow, your so smart!

How would you write that?

Thanks,

Patrick

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: adding a decimal amount to a date

Post by Simon » Mon Mar 04, 2013 6:13 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: adding a decimal amount to a date

Post by bn » Mon Mar 04, 2013 10:14 am

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

herbwords
Posts: 70
Joined: Sat Dec 01, 2007 2:59 am

Re: adding a decimal amount to a date

Post by herbwords » Mon Mar 04, 2013 6:18 pm

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

herbwords
Posts: 70
Joined: Sat Dec 01, 2007 2:59 am

Re: adding a decimal amount to a date

Post by herbwords » Mon Mar 04, 2013 6:58 pm

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.

jsburnett
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 121
Joined: Fri Mar 09, 2007 9:47 pm

Re: adding a decimal amount to a date

Post by jsburnett » Mon Mar 04, 2013 11:25 pm

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: adding a decimal amount to a date

Post by Simon » Mon Mar 04, 2013 11:58 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

herbwords
Posts: 70
Joined: Sat Dec 01, 2007 2:59 am

Re: adding a decimal amount to a date

Post by herbwords » Tue Mar 05, 2013 12:33 am

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: adding a decimal amount to a date

Post by mwieder » Tue Mar 05, 2013 12:54 am

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.

herbwords
Posts: 70
Joined: Sat Dec 01, 2007 2:59 am

Re: adding a decimal amount to a date

Post by herbwords » Tue Mar 05, 2013 1:14 am

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: adding a decimal amount to a date

Post by Simon » Tue Mar 05, 2013 1:39 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: adding a decimal amount to a date

Post by mwieder » Tue Mar 05, 2013 2:02 am

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"

herbwords
Posts: 70
Joined: Sat Dec 01, 2007 2:59 am

Re: adding a decimal amount to a date

Post by herbwords » Tue Mar 05, 2013 3:13 am

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

Post Reply