How do you do date maths

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
user#606
Posts: 217
Joined: Sun Jan 27, 2008 12:25 pm
Contact:

How do you do date maths

Post by user#606 » Mon Jan 21, 2013 8:43 pm

how do you add a number of days to the system date so the correct future date results?

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: How do you do date maths

Post by sturgis » Mon Jan 21, 2013 9:00 pm

The easiest way is to use dateitems.

Convert the date to dateitems, then add your days to item 3 (which I believe is days) then convert back.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: How do you do date maths

Post by dunbarx » Mon Jan 21, 2013 10:20 pm

As Sturgis said, dateItems gives you direct access to such things as the day of the week, month, year, etc.

But often, as you say, to do maths on the date, it is better to use "the seconds". This is a counter that started in 1970, and can be used to calculate dates directly.

So knowing that each day has 86,400 seconds is a way to find or set times and dates anytime in the past or future. The two are frequently used together, since converting from one format to the other is straightforward, and you can crunch your data in any way. Experiment with both, and convert from one to the other. These are very powerful tools.

Craig Newman

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: How do you do date maths

Post by sturgis » Mon Jan 21, 2013 11:13 pm

Using the seconds works well, the only thing you might have to watch out for is strangeness due to daylight savings time. Sometimes after you convert the date (which turns it into seconds for that date based at 12:00 PM) do your calcs, then turn it back in, losing the hour of daylight savings time will make the answer off by one hour. Easiest way to avoid this is add a couple hours to the seconds after the first convert.

user#606
Posts: 217
Joined: Sun Jan 27, 2008 12:25 pm
Contact:

Re: How do you do date maths

Post by user#606 » Tue Jan 22, 2013 12:32 am

I plumped for the seconds, because in my particular case (point taken about the drift) there is only going to be a year max, between dates.
All helpfull answers and gives the pros and cons for different uses.
Thank you for the help.

Post Reply