Convert from date to dateTime

I’m a bit stuck on how to convert this code from Date to DateTime:

Away_Return_Array(ArrayElement).year = Val(year(ArrayElement).Text)
Away_Return_Array(ArrayElement).month = month(ArrayElement).listIndex

You will want to create a new DateTime Object passing the year, month etc. Something like:

Away_Return_Array(ArrayElement) = New DateTime(year(ArrayElement).Text.ToInteger, month(ArrayElement).listIndex, day, TimeZone.Current)

1 Like

This line of code can become very long. Can individual date or time elements be changed or added? I would think New DateTime would not be used in that case, Is that so?

If you just do:

Var dt as DataTime

then all you have is a variable of type DateTime. To have that point at an actual instance of a DateTime object you’ve either got to New it or set it to refer to another instance of a DateTime. So either of these works, f’rinstance:

dt = new DateTime()
dt =

So can I change individual date or time elements after the instants is created and set to a value or do I need to set all elements ( year,month,day,hour,etc) at the same time?

You have to supply year, month, day as a minimum. If you just supply year, as in:

dt = new DateTime (2020)

then the 2020 will be taken as SecondsFrom1970. And it looks like you can’t supply y/m/d and then set hh:mm:ss subsequently, except by adding/subtracting a time interval from yoiur DateTime.

You should look up the DateTime docs, which will tell you this.

I think “Date” was easier to use in some cases. I have a mix of both in my program for that reason.

Yes, it was easier to use. It was also easier to get wrong. DateTime was meant to resolve some of those issues. I will continue using Date, though.