Incrementing dates

I have a certain date and I’m trying to increment it by 1 month. So if I have 1/1/2013 and I want to add 1 month to it, I would get 2/1/2013. This is what I’ve tried so far:

put "5" into x
put formattedTime ("%m/%d/%Y", x) into new
put "1/2/2013" into issueDate
put formattedTime ("%m/%d/%Y", issueDate) into newIssueDate
add newIssueDate + new to newIssueDate2
log newIssueDate

Right now, it just comes back with “01/02/2013.”

There’s no such thing as a month. A week is 7 days, a day is 24 hours, an hour is 60 minutes – these are all things that are defined, but a month is an almost arbitrary number of days. You actually have to write a function that checks whether the month is January and if it is, then is it after January 28, or January 29 if it’s a leap year, and if so, what do you do in terms of adding a month? Do you skip February and go to early March? Are you calling a month 30 days? What’s October 31 plus a month? And if the month in the current date is December, and you add a month to it, then you need to increment the year. There’s nothing simple about adding a month.

It’s true that adding “a month” to a date isn’t a well-defined operation. There is a technique you can use in SenseTalk though that will at least make it easy to do. Whether it will give you the result that you want in the difficult cases is for you to decide. But it will at least take care of the easy cases, and make the decisions for you in the tough ones.

This approach takes advantage of the “dateItems” format, which is a comma-separated list of numbers beginning with the year, month, and day:

put the dateItems -- shows the current date in dateItems format

You can convert any date to this format, change any of the item values by adding or subtracting any amount, then convert it back to a different date format. When converting a comma-separated set of values back to a date, SenseTalk will do its best to make sense of what’s there and do something reasonable. So month 13 becomes January of the following year, etc.

For your example, it would look something like this:

put "1/2/2013" into issueDate
convert issueDate to dateItems
add 1 to item 2 of issueDate -- add 1 to the month item
convert issueDate to long date

Instead of “long date” in the last line you can specify “date” or any of the dozens of other date formats that SenseTalk knows. Or use the formattedTime() function to convert it to any format you like.

Whoa! Slow down EggplantMatt…

Thanks SenseTalkDoug! That’s exactly what I needed to get the code to do what I wanted it to do, which was just increment to the next month.

This is the code I used to get it in the format I needed:

put "1/2/2013" into issueDate 
convert issueDate to dateItems 
add 1 to item 2 of issueDate -- add 1 to the month item 
put formattedTime ("%m/%d/%Y", issueDate) into newIssueDate

Which returns “02/02/2013”.

If this works for you, great, but I didn’t suggest it because it has the same problems that I described:

1/29/14 - 1/31/14 -> 3/1/14 - 3/3/14
10/31/14 -> 12/1/14
5/31/14 -> 7/1/14

It does handle the change in year when incrementing December dates and leap years. I recognize that it’s just a handful of dates, but in those cases, an entire month is skipped, and if you are stepping through the year, you go from dates at the end of the month, to dates at the beginning.