Date to Datetime, recommendations to change

I started bringing my app to API2 getting rid of decremented items.
Started at 18,205 warnings, now I’m at 11,962. All database stuff has been completed, next is date to datetime.

My programs tracks our membership for our society. As you can imagine Dates are used a lot.

I would like to know if anybody came across any gotch yas in converting to datetime.

Like I do this in a few place to get the last day of the month, would this work?:

Dim ldDateFrom As New Date( ldDate)
Dim ldDateTo as New Date( ldDate)
Dim ldToFilterDate as New Date

ldToFilterDate.Day = ldToFilterDate.Day + 1

ldDateFrom.Day = 1

ldDateTo.Month = ldDateTo.Month + 1
ldDateTo.Day = 1
ldDateTo.Day = - 1

Any other things to watch for.

As always thanks for the support of this forum.

This is how I would do it in API 2.0

Var ldDateFrom As DateTime
Var ldDateTo As DateTime
Var ldToFilterDate As DateTime

// Tomorrow
ldToFilterDate = DateTime.Now.AddInterval(0, 0, 1)

// First of this month at midnight
ldDateFrom = DateTime.Now.SubtractInterval(0, 0, DateTime.Now.Day - 1, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second)

// Last of this month at 23:59:59
ldDateTo = ldDateFrom.AddInterval(0, 1, -1, 23, 59, 59)
1 Like
  1. With Date I had my own routines to add/subtract times, but DateTime is much easier.
  2. I used to create a separate AddInterval object, but Wayne shows the better way.
  3. I still have a Method for Date>DateTime and DateTime>Date, for certain controls, but rarely use it.
  4. My SQLDatabaseMBS only accepts Date in the Prepared Statements, so I have changed these to accept the date fields as TEXT values instead.
  5. I miss having a TotalSeconds, but there are workarounds.

Having TotalSeconds or using TotalSeconds? I ask because we still have SecondsSince1970.

Yes, that is my workaround!

1 Like

Most of you probably know this but for those that don’t, we replaced Date with DateTime because:

  1. Users would often incorrectly modify dates and thus create errors in their apps.
  2. Date was not designed to handle things like time zones and Daylight Savings Time which also lead to subtle errors.

DateTime makes it nearly impossible to get any of this wrong. Yes, it’s a bit more work sometimes but what ultimately matters is the reliability of one’s code, not the number of lines.

And I like having SecondsSince1970, since it meant I could dispense with my own subclass of Date, to which I’d added that.

Thank you all. Are there any other things that might bite me?

While it’s been alluded to, it’s important to note that Date.TotalSeconds and DateTime.SecondsSince1970 are not the same because TotalSeconds used a different base date.

1 Like

How can I set a DateTime a specific date?

Var ldTargetDate As DateTime

ldToFilterDate.SQLDate = txtTarget.Value  //contains a SqlDate

Gets a compile error.
Cannot assign a value to this property

Here’s the documentation for DateTime: DateTime — Xojo documentation
There’s a constructor that accepts a few parameters you might find of interest.

Ye I got that, but what if I have a DT var and want to set that to a specific value?

Found it: DateTime.FromString(txtTarget.Value) //contains a SqlDate

1 Like