Adding days 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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
hrcap
Posts: 43
Joined: Mon Jan 14, 2019 5:20 pm

Adding days to a date

Post by hrcap » Fri Jul 05, 2019 3:17 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2070
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Adding days to a date

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

shiftLock happens

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2350
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Adding days to a date

Post by jmburnod » Fri Jul 05, 2019 3:44 pm

Hi,
First feeling: 2020 is a bissextil year :D
Best regards
Jean-Marc
Last edited by jmburnod on Fri Jul 05, 2019 4:02 pm, edited 1 time in total.

Klaus
Posts: 11058
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Adding days to a date

Post by Klaus » Fri Jul 05, 2019 3:46 pm

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

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 689
Joined: Fri Jun 27, 2008 9:00 pm

Re: Adding days to a date

Post by Mikey » Sat Jul 06, 2019 2:24 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2070
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Adding days to a date

Post by [-hh] » Sat Jul 06, 2019 3:14 pm

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
shiftLock happens

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 689
Joined: Fri Jun 27, 2008 9:00 pm

Re: Adding days to a date

Post by Mikey » Sat Jul 06, 2019 3:46 pm

Yes but the rest of you already dealt with that, so I was just on mop-up duty.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”