DateTime: how to set a day ? or an hour?

just playing with datetime instead of date object
as all the properties are read only, how do you set a specific time to a datetime object ?
:frowning:

Constructor or DateInterval

1 Like

Just examples:

Var d As New DateTime(2020, 01, 05, 14, 23, 05)

or

Var c As DateTime
c = DateTime.FromString("2021-03-04 09:00:55")

or

Var k As  DateTime = DateTime.Now.SubtractInterval(0,4,1)

let’s say I want to keep the date, but only change the hour to 8 and the minutes to 0
it’s really complicated compared to the “old” Date object…
I just don’t understand WHY these datetime properties are read only …

The short answer is that the objects are now “immutable” – that is to say can’t be altered – so you just have to change your mindset and create new objects when you need a different time. DateInterval can be your friend here, or other ways you can construct a new DateTime object.

It’s not so much complicated as it is just a different way of thinking, when you are used to the older Date object methods.

1 Like

One way would be:

Var k As  DateTime =  DateTime.FromString(DateTime.Now.SQLDate + " 08:00:00")
1 Like

Dim newdate As new DateTime(OldDate.Year, OldDate.Month, OldDate.Day, 8, 0)

2 Likes