Convert old Date to Xojo.Core.Date

I know I’m overthinking this one but how would I go about casting an old Date object to the new Xojo.Core.Date object?

I purchased a control (encrypted) that returns the selected date (old object) but date manipulation routines I have created use the new framework. Not sure how to get the returned date into a new Xojo.Core.Date.

I just tested this and I am also researching any built in functions to see if there is a more efficient way to accomplish this.

HTH. (I tied this to a button action to test quickly).

  Dim d as New Date
  Dim dString as String = d.SQLDateTime
  Using Xojo.Core
  Dim dateValue As Text = dString.ToText
  Dim myDate As Date = Date.FromText(dateValue)
  MsgBox "New Framework Date: " + myDate.ToText

[quote=209136:@Scott Rich]I know I’m overthinking this one but how would I go about casting an old Date object to the new Xojo.Core.Date object?
You cant cast one to the other
You can convert one to / from the other but it involves code like Mike posted

Dim d As New Xojo.Core.Date(aClassicFrameworkDate.TotalSeconds - 2082844800, Xojo.Core.TimeZone.Current)

Eli thank you!

Make sure you consider time zone when you make this conversion. The old date format wasn’t very particular about them, but the new one is.

I’m not sure where this came from, but this consistently brings back the wrong date for me. For example, if the date in the database is 1962-02-18 00:00:00 it likes to bring back 1962-02-17 00:00:00. Has anyone else experienced this?

Classic dates are GMT dates I assume. So you would need to apply the offset:

Dim d As New Xojo.Core.Date(aClassicFrameworkDate.TotalSeconds - 2082844800 - Xojo.Core.TimeZone.Current.SecondsFromGMT, Xojo.Core.TimeZone.Current)

And some time zones have daylight saving time – I don’t know how these influence TimeZone and SecondsFromGMT.

Actually Classic date is using the current Locale.

dim d as new date system.debuglog d.SQLDateTime Dim dada as new, d.month,, d.hour, d.Minute, d.Second, 0, Xojo.Core.TimeZone.Current) system.debuglog dada.totext

I had a bug report from (other peoples) code that used the above snippet and found that the above snippet consistently brought back the wrong date. No big deal to move it to using the global Date object. This is a warning for others that might use it verbatim as the person who wrote this code did.

Which snippet ? The totalsecond one, or the constructor with year, month, day, hour, etc ?

Whichever one it seems pretty easy to verify after constructing the Xojo.Core.Date if it is in sync with Classic. Could it rather not be an issue with the conversion between totalsecond and secondsFrom1970 ?

The first snippet. And yes, it’s a conversion issue between total second’s and secondsFrom1970.

The OPC project I was working on I could tell that the programmer took the code, verbatim, from this post and used it without checking its authenticity. The dangers of using code you find online.

What’s the best way to go in reverse? I need to go from a Xojo.Core.Date to the classic Date.

I tend to use exactly the same way I posted : use the constructor with explicit values. But the root property is probably OK too.

the classic date constructor with appropriate params passed seems simple enough

Guilty :slight_smile:

Tested, but, obviously not extensively enough.

[quote=273300:@Peter Fargo]Guilty :slight_smile:

Tested, but, obviously not extensively enough.[/quote]

No worries. Easy to miss because it does bring back a date that looks good at first blush. Unfortunately, a client was the one that spotted it. :frowning:

Using TimeControl and DateControl (EinHugur Plugin), i try to get time and Date valueChanged
These values changes only inside valueChanged Event (Date and Time ctrl), i would like to get the values changed every where in the project.

I am not sure what you are asking you get notify in the event when the value changes and you there notify the rest of your application and variables of the change.

If you for any reason want to read the value at random time then you just read the controls DateValue property.

Basically same as any other control works

You should read about AddHandler. It allows you to reroute an event to another place.