Xojo2023r3: Using Date

Following code in web page method:
Var vStartDate As New Date
Var vEndDate As New Date

vStartDate.Day = mDate.Day
vEndDate = mDate

where mDate is of Date type and is property of the web page, works both in Xojo2018 and Xojo2023r3.

However, I have run the Analyze menu on the web page and it lists the warning that Date should be replaced with DateTime. I can easily change the Date to DateTime type but then the line:
Var vStartDate As New DateTime
will generate compilation error. When changed to
Var vStartDate As DateTime
it will not generate comilation error but then
vStartDate.Day = mDate.Day
will produce compilation error as Day attribute is Read Only.

Should I forget about the Analyze warning and do nothing
should I fix it, but then how?

Please help.

Date and DateTime are different classes and not direct replacements.

If you want to update to DateTime you will need to learn the differences and change your code to be able to use DateTime.

Some things are Read Only now, so you may need to construct a new DateTime if needed or learn to use DateInterval.

Edit, an example with Date:

Var mDate As New Date(2023, 11, 10) //create a date for 2023-11-10

Var vStartDate As New Date //create a Date as today
Var vEndDate As New Date //create a Date as today

vStartDate.Day = mDate.Day //change the day to mDate.Day
vEndDate = mDate //make vEndDate the same as mDate

will be something like this with DateTime:

Var mDate As New DateTime(2023, 11, 10) //create a DateTime for 2023-11-10

Var vStartDate As New DateTime(DateTime.Now.Year, DateTime.Now.Month, mDate.Day) //create vStartDate with today's year, today's month and mDate.Day
Var vEndDate As New DateTime(mDate) //make vEndDate the same as mDate

Alberto, thanks for the example, as you can imagine there is a bit more code pertaining to date manipulation in the legacy code I am working on, so nothing is ever simple, I will resist the urge of “fixing” it by simple substitution right away, thanks.

Hi Greg.

First, please remember to use the code tags when posting code as it makes it easier to read.

The advantage of DateTime over Date is it’s immutable so you can’t accidentally do something that unintentionally changes a value.

For example, this is possible with a Date:

var d1 as new Date // Today
var d2 as Date = d1 // Also today

d1.Day = 1
d1.Year = 2000

s = d2.SQLDate // now 2000-10-01

You can still use Date if you prefer and ignore the warnings. You can also create a DateTime from a Date through the Constructor:

// d is a Date
var dt as new DateTime( d )

Kem, thanks for the info. As I am new to Xojo and this forum specifically - where do I find the info on code tags and how to use them? I will educate myself as long as I know what is needed and where to get it.

I don’t necessarily insist on “fixing” all the warnings but I would like to do it as the legacy code is a mix of old/new and it is simply too confusing to work with.

When starting a post, there is a toolbar right above the text area. The icon that looks like “</>” will give you code tags. Or you can simply type three backticks above and below your code block, which is really all that button does anyway.

Ok, great, easy enough. Just as test, why this

Var vEndDate As New DateTime

generates compilation error and this

Var vEndDate As DateTime

does not.

New DateTime expects parameters, either a Date or a DateTime to copy from, or the values for Year, Month, etc., or seconds.

var dt as DateTime

dt = new DateTime( someDate )
dt = new DateTime( someDateTime )
dt = new DateTime( 2023, 10, 17, 0, 0, 0, 0 )
dt = new DateTime( 1000000.0 ) // Seconds from 1970

See: DateTime — Xojo documentation

The equivalent of New Date would be:

var vEndDate as DateTime = DateTime.Now

Once you create a DateTime, you cannot change it, only create a new one to replace it.

OK, one of the reasons for the Date in the code I am working on was to take the input date from dialog page from the user to search database. The date picker widget used on the dialog page would return the starting date, the ending date might be assumed as start date + 1 or start date + 7. In any case the time part was irrelevant. I didn’t get to it yet but I will also need to know what is the new date picker widget that I need to use in Xojo2023r3 and how to use it.

For that task you may be interested in DateInterval which is for shifting DateTime by your desired interval.