How to Increase a Date by Specific Intervals

Hey all,

I’m hoping to be able to increase dates by frequencies. so right now i have a date in a string as follows:

(YYYY-MM-DD HH:MM:SS) - i can change the format any way i need to to get this to work though.

i want to be able to take a date (2018-01-18 12:00:00) and increase it by a week -> (2018-01-25 12:00:00)

The problem that i am having is that if i split the string into the different date components (year, month, day, etc) and add 7 to the days, will it automatically adjust the month and year if these are subject to change? so if i have a date like (2018-01-31 12:00:00) and add a week to that would it adjust the month to 02? also would it adjust the date to 07 rather than 38?

If anyone has any suggestions how i can accomplish this it would be great. the week and quarterly intervals are the ones im struggling with the most, day, month, and year should be simple - as long as the other date components talk to each other to adjust based on month and year transitions.

I would suggest taking a look at the new framework date. You adjust them using DateIntervals so it’s quite easy to add a week to an existing date.

http://developer.xojo.com/xojo-core-date
http://developer.xojo.com/xojo-core-dateinterval

[quote=369654:@James Rogers]The problem that i am having is that if i split the string into the different date components (year, month, day, etc) and add 7 to the days, will it automatically adjust the month and year if these are subject to change? so if i have a date like (2018-01-31 12:00:00) and add a week to that would it adjust the month to 02? also would it adjust the date to 07 rather than 38?
[/quote]
yes it will. and it even works in the old framework.
parse the date using myDate.sqldatetime or using the parsedate method.
then add the number of seconds (one day is 86400 seconds) to the totalseconds property of the date.

myDate.totalseconds = myDate.totalseconds + (number_of_days * 86400)

the correct day,month,year will be calculed by xojo.

myDate.day = myDate.day + number_of_days

works just as well… And YES it does modify the month and/or year if required

For just a quick fix: assign your string to myDate.SQLDateTime, no need to split your string or convert the format, then add the days as Dave said or the seconds per day as Jean-Yves said.
https://documentation.xojo.com/index.php/Date.SQLDateTime

If you want to use iOS is better to learn Xojo Core Date as Tim recommended.