I have dates in a Text file and I want to “validate” them in storing them in a DateTime object before display.
One problem is… I lost my hair trying to apply a date in a string to a DateTime variable.
Some kind soul with help is needed.
I have dates in a Text file and I want to “validate” them in storing them in a DateTime object before display.
One problem is… I lost my hair trying to apply a date in a string to a DateTime variable.
Some kind soul with help is needed.
Var s As String = "2024-11-28"
Var dt As DateTime = DateTime.FromString(s, Nil, Nil)
This is strange as you usually read and recommend reading the Documentation:
https://documentation.xojo.com/api/data_types/datetime.html#datetime-fromstring
One of the examples show:
Var SQLDate As String = "2019-08-01"
Var myDate2 As DateTime = DateTime.FromString(SQLDate)
is this what you want or something else?
You will get a RuntimeException if you try something like “2019-13-52”
You’re lucky that we don’t just refer to the instructions like you usually do.
Try
d = DateTime.FromString(YourString, Locale.Current)
Catch err As RuntimeException
// Handle exceptions here...
End Try
From the docs but enhanced:
DateTime.FromString(dateValue As String, locale As Locale = Nil, timeZone As TimeZone = Nil As DateTime
If you do not pass in a time zone (or it is Nil), the resulting date is in the current time zone.
When no locale is specified (or it is Nil), the string dateValue parameter can be in either SQLDate (YYYY-MM-DD) or SQLDateTime (YYYY-MM-DD HH:MM:SS) formats.
That said, those variants convert OK values as YYYY-MM-DD at 00:00:00 hour of the current timezone:
And this one is used to convert from the local format, like DD/MM/YYYY or DD/MM/YY when your system is set to the Brazil locale:
I do that because the answer usually is there and still usually, the op do nt rerad the documentation.
in this case I wasted I do not know how may times on it to fail/no result !
No locale needed, so I set it Nil. In standard API it was so easy !
With the current versions (2024r3.1/sequoia)…
Thanks for the answers guys.
That’s just an assumption. You can’t know for sure. And Xojo’s documentation isn’t particularly exemplary.
And you did not consult the docs (i assume).
Everything is fine. It’s just funny when you have such a simple question, a question on that you would send others straight to the documentation.
Here’s my final code:
Var SQLDate As String = Cell_Cont
Var Cell_Date As DateTime = DateTime.FromString(SQLDate)
Var Foo As String
foo = Cell_Date.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.Full)
And I do not get the date as I want: specifically, as the documentation says, the name of the day…
I get dd-mm-yyyy, etc.
To be sure, I changed my code a bit to:
Var SQLDate As String = Cell_Cont
Var Cell_Date As DateTime = DateTime.FromString(SQLDate)
Var Final_Str As String
Final_Str = Cell_Date.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.Full)
and get:
Xojo 2024r3.1
Sequoia 15.1.1 (24B91)
A bit I forgot… Most of the time, I search in the docs (sometimes I even check with the IDE what I found there) before sending the oPto the documentation. Look at the Custom Row Sort (for numbers) question from yesterday, I think.
Sometimes, I even get flags… as Thank You…
You pass a FormatStyle.Short to the date part of the date time. I guess you mixed up both format parameters.
As long as you are generally friendly and obviously helpful, you shouldn’t worry about these flags. … The Xojo team certainly recognizes when a flag is justified and when there is probably a misunderstanding behind it.
Besides, we know you well enough to know that you just want to help and that English is not your first language.
I tried all DateTime.FormatStyles
entries from the documentation:
System.DebugLog "Short: " + Cell_Date.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.Short)
System.DebugLog "Medium: " + Cell_Date.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.Medium)
System.DebugLog "Long: " + Cell_Date.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.Long)
System.DebugLog "Full: " + Cell_Date.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.Full)
System.DebugLog "None: " + Cell_Date.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.None) here’s the answers:
Text debug
: Short: 10/08/1968 00:00
: Medium: 10/08/1968 00:00:00
: Long: 10/08/1968 00:00:00 UTC+1
: Full: 10/08/1968 00:00:00 UTC+01:00
: None: 10/08/1968
: Nil+Long: 10/08/1968 00:00:00 UTC+1
The Nil + Long is just another check case…
Am I doing something wrong ?
PS: this is inserted in an old project, using a sample .sqlite
file who have dates for testing purposes.
I want to implement a Sort on dates…
Yes, you always use DateTime.FormatStyles.Short for the second parameter that is the Date part. You are only changing the third parameter that is Time.
You can also use a Format that can override the Style. See the docs here. Specially the talk about ICU Date/Time Format.
Edit: changing your code to:
System.DebugLog "Short: " + Cell_Date.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.Short)
System.DebugLog "Medium: " + Cell_Date.ToString(Locale.Current, DateTime.FormatStyles.Medium, DateTime.FormatStyles.Medium)
System.DebugLog "Long: " + Cell_Date.ToString(Locale.Current, DateTime.FormatStyles.Long, DateTime.FormatStyles.Long)
System.DebugLog "Full: " + Cell_Date.ToString(Locale.Current, DateTime.FormatStyles.Full, DateTime.FormatStyles.Full)
System.DebugLog "None: " + Cell_Date.ToString(Locale.Current, DateTime.FormatStyles.None, DateTime.FormatStyles.None)
I get this:
: Short: 10/10/68, 12:00 AM
: Medium: Oct 10, 1968 at 12:00:00 AM
: Long: October 10, 1968 at 12:00:00 AM GMT-5
: Full: Thursday, October 10, 1968 at 12:00:00 AM GMT-05:00
: None:
From your message, I understand (was it too small in the IDE for my eyes ?) I do not noticed (nor understand) the repetition !
Thank you Alberto.
First, what exactly are you wanting to achieve?
I guess you are getting a string “1968-08-10” (YYYY-MM-DD) and want to get a DataTime object from it, and later some other string… What final string based on the example “1968-08-10” input date?
The screenshot shows what is wanted for the Date part.
You want that long english string? Or just the week name of the day? The week name in English? French? Any language?
Var sqldate As String = "2019-08-01"
Var dt As DateTime = DateTime.FromString(sqldate) // Perfectly converted SQL to local standards
Var weekDayNumber As Integer = dt.DayOfWeek // Just an information
// Let's extract the day of the week
Var dateFull As String = dt.ToString(DateTime.FormatStyles.Full, DateTime.FormatStyles.None)
Var weekDaySize As Integer = dateFull.IndexOf(",")
If weekDaySize<0 Then weekDaySize = dateFull.IndexOf(" ")
Var weekDay As String = dateFull.Left(weekDaySize) // From current locale
break
Works for most European languages.
Why not:
Var sqldate As String = "2019-08-01"
Var dt As DateTime = DateTime.FromString(sqldate) // Perfectly converted SQL to local standards
// get day of the week
Var weekDay As String = dt.ToString("cccc")
break
I’ve decided that mm-dd-yyyy is a really silly way to display dates. In fact I just came across a site that said the the U.S. is the only country to use that format. So, when I show a date on the screen I use yyyy-mm-dd, which is obvious and sorts correctly.
So far no one has complained (could be that no one is using my stuff).