Page 1 of 1

Adding days to a date

Posted: Fri Jul 05, 2019 3:17 pm
by hrcap
Good Afternoon

I am having trouble adding a number of days to a date.

In the example below I start with a date of 4/7/19 and add 365 days to it, I would expect the answer to come out as 4/7/20 but instead the answer is coming out as 4/6/20 and I cannot work out why.


Any help would be much appreciated.


Many Thanks

Code: Select all

on mouseup
   put "4/7/19" into t_date
   
   put "365" into t_days_valid_for
   
   
   Convert t_date from date to seconds
   
   put t_days_valid_for * "86400" into t_days_valid_for --this converts the number of days valid for to seconds
   
   
   put t_date + t_days_valid_for into t_expiry_date
   
   convert t_expiry_date to date
   
   answer t_expiry_date
   
end mouseup

Re: Adding days to a date

Posted: Fri Jul 05, 2019 3:34 pm
by [-hh]

Re: Adding days to a date

Posted: Fri Jul 05, 2019 3:44 pm
by jmburnod
Hi,
First feeling: 2020 is a bissextil year :D
Best regards
Jean-Marc

Re: Adding days to a date

Posted: Fri Jul 05, 2019 3:46 pm
by Klaus
Hi hrcap,

we do not have a LEAP year in 2019. Try with
-> 4/7/20
and you will get
-> 4/7/21
because 2021 is in fact a leap year. :D


Best

Klaus

Re: Adding days to a date

Posted: Sat Jul 06, 2019 2:24 pm
by Mikey
Fun fact and imho a better way to do date math: use dateItems. All math works great and no extra math like dealing with seconds

Code: Select all

put "4/7/19" into tDate
convert tDate to dateItems
add 365 to item 3 of tDate
convert tDate to short date

Re: Adding days to a date

Posted: Sat Jul 06, 2019 3:14 pm
by [-hh]
The problem is not the method but the fact, that the days range in question contains the leap day Febr 29.
So going on one year (what the OP obviously wants) requires to add 366 days.
Or, with your method

Code: Select all

add 1 to item 1 of tDate

Re: Adding days to a date

Posted: Sat Jul 06, 2019 3:46 pm
by Mikey
Yes but the rest of you already dealt with that, so I was just on mop-up duty.