Dates

Greetings…

To make a very long and frustrating story extremely short…

How do I do this in the new DateTime code? I have a little over 800 lines to correct.

dim stime as date
stime.hour = val(left(editfield3.text,2))
stime.minute = val(mid(editfield3.text,3,2))
stime.second = 00

Background… Pulling mostly time values from databases, internet, and textfields. Then making comparisons to make further decisions as what to do with them. Most values pulled from the databases are in the form of just times: 22:00 - 03:00. I create a start date/time UTC, then an end date/time UTC, then compare current date/time in UTC to see if it falls within the start end dates/times. I am trying not to do a complete rewrite, as I can not deliver any code with depreciated objects per my contract. The bottom line, I MUST remove all depreciations in order to get paid.

Thank you everyone for your time and efforts reading this.

Var d As New DateTime(0, 0, 0, stime.hour, stime.minute, 0, localtimezone )

should do it.

If you need to print it, then something like this should do it also:

Var T as String T = d.ToString(Locale.Current, DateTime.FormatStyles.None, DateTime.FormatStyles.Medium)

From memory - I am not at my workstation, so some corrections may be needed.

Thank you for your time…

Unfortunately, it didn’t work.

Window3.Open, line 20
This item does not exist
Var d As New DateTime(0, 0, 0, stime.hour, stime.minute, stime.second,localtimezone)

Made one slight change to use Locale.Current same result.

Window3.Open, line 20
There is more than one method with this name but this does not match any of the available signatures.
Var d As New DateTime(0, 0, 0, stime.hour, stime.minute, stime.second, Locale.Current)

Window3.Open, line 20
d is an unused local variable
Var d As New DateTime(0, 0, 0, stime.hour, stime.minute, stime.second,Locale.Current)

Oh, I see what I did… Somehow I thought that you were defining a new time with sTime data. You want to define stime! This should work:

Var stime  As New DateTime(0, 0, 0, val(left(editfield3.text,2)), val(mid(editfield3.text,3,2)), 0, Locale.Current )

You replace all this with the above:

dim stime as date stime.hour = val(left(editfield3.text,2)) stime.minute = val(mid(editfield3.text,3,2)) stime.second = 00

This would be a direct replacement for your original post assuming your time is formated like 10:30

Var d As New DateTime( 1, 1, 1, editfield3.Text.Left(2).ToInteger, editfield3.Text.Middle(3,2).ToInteger, 0, 0, TimeZone.Current )

This should work for creating a string representation.

[quote=487296:@Louis Desjardins]Var T as String T = d.ToString(Locale.Current, DateTime.FormatStyles.None, DateTime.FormatStyles.Medium)[/quote]

Notes:
Left and Mid as global methods have been deprecated. They should be replaced with String.Left and String.Middle but String.Middle is now 0-based while Mid was 1-based.

The Global.Mid to String.Middle change may mean you need to change editfield3.Text.Middle(3, 2) to editfield3.Text.Middle(4, 2) depending on how your time is formatted.

There is a nanoseconds parameter missing in Louis’s original message that may be giving you the compilation errors and the final param is a Timezone not a Locale.

Additionally, if I recall correctly, 0 values for year, month and day are illegal for DateTime.

this is what happens when going from memory. Old habits die hard! Thank you @Jonathan Eisen

Don’t ask me how long it took to relearn that.

Ok, so… This worked, so I thought.

Var stime As New DateTime( utcdate.year, utcdate.month, utcdate.day, editfield3.Value.Left(2).ToInteger, editfield3.Value.Middle(2,2).ToInteger, 0, 0, TimeZone.Current)

Var rtime As New DateTime( utcdate.year, utcdate.month, utcdate.day, editfield3.Value.Middle(5,2).ToInteger, editfield3.Value.Right(2).ToInteger, 0, 0, TimeZone.Current)

Until I got here. I don’t understand how it can not exist when it’s defined right there!

Select Case utcdate.hour <- defined as a depreciated date, works great.
Case 12 To 23 'PM
If rtime.hour < stime.hour Then <- Error, rtime does not exist??
//rtime = rtime.AddInterval(0,0, 1) 'Add a day
End If
Case 0 To 11 'AM
If rtime < stime Then
//stime = stime.SubtractInterval(0,0, 1) 'Subtract a day
End If
End Select

Window3.Open, line 63
This item does not exist
If rtime.hour < stime.hour Then

[quote=487325:@Jeff Stillinger]Ok, so… This worked, so I thought.

Var stime As New DateTime( utcdate.year, utcdate.month, utcdate.day, editfield3.Value.Left(2).ToInteger, editfield3.Value.Middle(2,2).ToInteger, 0, 0, TimeZone.Current)

Var rtime As New DateTime( utcdate.year, utcdate.month, utcdate.day, editfield3.Value.Middle(5,2).ToInteger, editfield3.Value.Right(2).ToInteger, 0, 0, TimeZone.Current)

Until I got here. I don’t understand how it can not exist when it’s defined right there!

Select Case utcdate.hour <- defined as a depreciated date, works great.
Case 12 To 23 'PM
If rtime.hour < stime.hour Then <- Error, rtime does not exist??
//rtime = rtime.AddInterval(0,0, 1) 'Add a day
End If
Case 0 To 11 'AM
If rtime < stime Then
//stime = stime.SubtractInterval(0,0, 1) 'Subtract a day
End If
End Select

Window3.Open, line 63
This item does not exist
If rtime.hour < stime.hour Then[/quote]

It would be helpful to see more of what you’re trying to do here. It could be that rtime or stime are out of scope or plenty of other things but without more context we will just be shooting in the dark.

[quote=487325:@Jeff Stillinger]Ok, so… This worked, so I thought.

Var stime As New DateTime( utcdate.year, utcdate.month, utcdate.day, editfield3.Value.Left(2).ToInteger, editfield3.Value.Middle(2,2).ToInteger, 0, 0, TimeZone.Current)

Var rtime As New DateTime( utcdate.year, utcdate.month, utcdate.day, editfield3.Value.Middle(5,2).ToInteger, editfield3.Value.Right(2).ToInteger, 0, 0, TimeZone.Current)

Until I got here. I don’t understand how it can not exist when it’s defined right there!

Select Case utcdate.hour <- defined as a depreciated date, works great.
Case 12 To 23 'PM
If rtime.hour < stime.hour Then <- Error, rtime does not exist??
//rtime = rtime.AddInterval(0,0, 1) 'Add a day
End If
Case 0 To 11 'AM
If rtime < stime Then
//stime = stime.SubtractInterval(0,0, 1) 'Subtract a day
End If
End Select

Window3.Open, line 63
This item does not exist
If rtime.hour < stime.hour Then[/quote]
Copied your code, I don’t get an error:

[code]Var utcdate As New date

Var stime As New DateTime( utcdate.year, utcdate.month, utcdate.day, editfield3.Value.Left(2).ToInteger, editfield3.Value.Middle(2,2).ToInteger, 0, 0, TimeZone.Current)
Var rtime As New DateTime( utcdate.year, utcdate.month, utcdate.day, editfield3.Value.Middle(5,2).ToInteger, editfield3.Value.Right(2).ToInteger, 0, 0, TimeZone.Current)

Select Case utcdate.hour // defined As a depreciated date, works great.
Case 12 To 23 'PM
If rtime.hour < stime.hour Then // No error
rtime = rtime.AddInterval(0,0, 1) 'Add a day
End If
Case 0 To 11 'AM
If rtime < stime Then
stime = stime.SubtractInterval(0,0, 1) 'Subtract a day
End If
End Select[/code]

I think I found the error. Seems the new stuff doesn’t know that 2400 is actually midnight, or 0000. The old date could handle 2400 just fine. The new format does not. I have about a half million database times as 2400. Try it with an input date (editfield3) being 2100-2400 which is a very common entry in the German and Russian databases I use. It will throw an error of being out of date range.

Thought I would try to trap the 2400 before the var statement.

I am going to keep pounding on it. Two lines down, about 798 to go. WEEE!!!

When did you get that error? At design or run time?

That sounds like a bug that should be reported…

Run time. Selected all kinds of times and adjusted dates perfectly until I loaded the German database. Where, they use 2400 as midnight. It threw the error.

Got it. The error was because the object failed to be created. Interesting that it didn’t fail on the constructor line. I assume you’re just converting it to 0000 if the value is 2400.

As I said, it sounds like a bug report should be filed.

Not an error, but EditFields do not exists since Xojo first release (2014) or more.

Maybe a whole (and real) refactoring will be a good idea (if release time permit).

Also, do you know of:
http://documentation.xojo.com/api/data_types/datetime.html#datetime-fromstring

This allows you to get a DateTime (thingk old Date) from your date time text string variable and will remove your Left/ Mid lines to get a date atom (Year, Month, Day, Hours…), eventually.

Depending on the number of errors / deprecations / removals reported at build attempt, a reformat (as I already wrote above) have to be done:
Print the project source to PDF,
Start a brand new Xojo 2019r3.1 project, *
Populate it with a brand new writing of the old code,
Add comments where you actually have troubles to understand (now),
Use TextArea Class (in fact, give them a meaningful name),
and so on.

I had recently to modify an old project because the target web page changes, and I get troubles to make it work again until I re wrote the offending method with better comments,better name of the Controls, and so on. It now works (but very slow: work in progress / optimization to come!).

  • The Language Reference will only show Xojo 2019r3.1 available stuff in auto complete.

I get the fail on the constructor line.

DateTime only handles valid ranges. Date allows invalid ranges and tries to create a “valid” date from the information we provide. You can do this with Date:

Var myc As New date myc.SQLDateTime = "2020-05-04 28:75:99" //myc.SQLDateTime will be 2020-05-05 05:16:39

And 2020-05-04 24:00:00 will be converted to 2020-05-05 00:00:00, I guess there is no 24:00. In other words, be careful if you want to change 2400 to 0000, you will need to add 1 day.