To increment a date?

Hi folks
Can anyone tell me if there is a simple way to increment a date?
That is, given a date in one variable, can a second one be computed for the next day, obviously with month and/or year incremented if appropriate.

since dates are objects they need to be handled a bit differently than scalars (integer, float etc)

``````dim dt as new date
dt.day=dt.day+1``````

this works… but if you want to create a 2nd object then you have to instantiate it as well

``````dim dt as new date
dim dt1 as new date
dt1.day=dt.day+1``````

if you say this

``````dim dt as new date
dim dt1 as date '' NO NEW

dt1=dt   ' creates TWO pointers to SAME date object

dt1.day=dt1.day+1  ' increments BOTH dt and dt1``````

I know this doesn’t answer your question directly… but should provide enough insight

For the Language Reference for Date:

[quote]When you create and instantiate a Date object, it is initialized to the current date and time. Therefore the order of assignment is important: when dealing with a day that exists in one month when the current system date is in another (such as December 31 when you’re in November), setting the Day before the month can result in an unexpected (here 12/01/2011) or even invalid date. Not setting the year before entering Feb 29 will cause oddities if the current year is not a leap year.
[/quote]

[quote]The TotalSeconds property is the master property that stores the date/time associated with a Date. The other property values are derived from TotalSeconds. If you change the value of the TotalSeconds property, the values of the Year, Month, Day, Hour, Minute, and Second properties change to reflect the second on which TotalSeconds occurs. Conversely, if you change any of these properties, the value of TotalSeconds changes commensurately.
[/quote]

The safest way to add a day to a date is:

```dt1.TotoalSeconds = dt1.TotalSeconds + 60*60*24 ```

that is NO different that dt1.day=dt1.day+1
and a whole lot more work

one method is no safer than the other… the same restriction about changing day before month still applies.

and I have never had it create an INVALID date… the wrong date? sure… but never invalid

Thank you folks
I should have checked properly in the first place - I assumed that this would only add to the day and result in an incorrect date. My apologies.

Dave is right! I just always use TotalSeconds so I don’t have to think about order.

I don’t know if it’s still there, but there was a bug in the Windows version when manipulating properties other than TotalSeconds, so I always use TotalSeconds too.

I wanted to create a subject about those things. It’s the same with the Array. If I do MyArray1 = MyArray2 then they are linked, if I modify one of the two arrays, the second is modified too. Same with folderitem isn’t it?

For the dates, if we use .Totalseconds then we don’t have this problem.

Rule of thumb is … if you need to use NEW, then you have a pointer, and need to be careful how you copy/clone the object

Folderitems are objects… but are auto-instantiating… (ie… no NEW is used) therefore they do not behave the same way.

I’m not sure what you mean by this. If I do this:

``````dim d1 as new date
dim d2 as date = d1
d1.TotalSeconds = 100
// d2.TotalSeconds = 100 too``````

[quote=51157:@Kem Tekinay]I’m not sure what you mean by this. If I do this:

``` dim d1 as new date dim d2 as date = d1 d1.TotalSeconds = 100 // d2.TotalSeconds = 100 too ```[/quote]

As well you should have the problem… D2 has become a pointer to D1… using TOTALSECONDS does not remove that issue. TotalSeconds is another object property… just like DAY, MONTH or YEAR is…

Guys, this is what I use. Returns a new date object without a reference to the first:

[code]Function NextDay(extends d as Date) As date
dim d2 as new Date(d)

d2.TotalSeconds = d2.TotalSeconds + 86400

return d2
End Function
[/code]

Right, that version of the Date constructor creates a clone of the given date.

If someone wants to test the latest version of Xojo in Windows for the bug I mentioned, the report and project is here:

The bug is still there in Xojo 2013r3.3, Windows 7, 64-bit. When you create a New Date, the TotalSeconds has a fractional component, which is dropped after `Day = Day - 1`. That makes total sense, because it has to recalculate TotalSeconds from values that are all integers. The question in my mind is, Why does TotalSeconds have a fractional part in the first place? Shouldn’t it always be an even integer value? Date.Second always returns an integer - no fractional part.

Effectivly, I answered from iPad without look my code, my memory was bad. I do, when I already have a date in DateDuJour :

```Dim vTtVar as Variant vTtVar = DateDuJour.SQLDateTime DateLaunch = vTtVar.DateValue ```
I don’t know if it’s hard to add in the IDE, but in the case of date, array, etc. , when the two variable are linked it should write another thing than just = .
If I do :
MyIntegerA = MyIntegerB , it is MyIntegerA <- MyIntegerB
but if I do :
MyArrayA = MyArrayB then they are linked, the IDE should write something like
MyArrayA == MyArrayB

It cause me some trouble until I realize that in some case the variables were linked.

No… the IDE should never second guess the developer.

If you want a new object… create a new object.

It is your responsiblity to be aware of the consequences of using scalar vs objects

It could help if the IDE make difference between scalar and object. But  now I know

I’d prefer if there were no scalars, but it’s too late for that now.

I wrote above “Now I know”, but in fact I don’t really know. This specific things for scalar are with :

• Date
• Array
• Folderitem
• What else ? (No, I’m not Georges Clooney, it’s really me on the photo beside)