Format date

Hello group, at the moment I load the date directly from the database into a listbox where it is stored in the following format. Could I insert something like this into this line of code, to tell it to convert the date into the local format of the PC in use? or something like that without having to declare too many variables?

MyListBox.AddRow(rows2.Column("Data").DateTimeValue.SQLDate)

In the list box: 2017-05-01 but i need 01-05-2017
Is it possible to have the conversion already in the data addition line or do I necessarily have to convert the date outside the loop into a variable and when adding the date in the listbox do I have to insert this updated variable?
I hope I explained myself clearly…

something like that Format(rows2.Column(“Data”).DateTimeValue,“mm-dd-yyyy”)

You can use .ToString:
https://documentation.xojo.com/api/data_types/datetime.html#datetime-tostring

Read this paragraph:

The format parameter is expected to be ICU Date/Time Format syntax. For example, MM-dd-yy for the date and hh:mm:ss for the time, or MM-dd-yy hh:mm:ss for both. You can use yyyy to display a four digit year. The format string is case-sensitive. MM must be used for date.

And look at the format options on the linked page.

You can use .ToString(“MM-dd-yyyy”)

Edit: but to display MM-dd-yyyy for US users and dd-MM-yyyy (for others) maybe is better to use locacle.current

d.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.None)
1 Like

rows.Column(“Data”).DateTimeValue.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.None)

And if with this code I have the date 02/12/2024 and I wanted to have that in English format, could I replace Locale.Current with something else?

I test Locale(“en-US”), but not work :frowning:

Can you provide more information?
Like:

  • the code you used
  • the error you got (if any)
  • screenshot of the debugger

it could be as simple as you didn’t use ‘new’ for the locale.

Edit: my guess is that you are getting this:

and you need to put new before locale.

If you show us the error instead of just writing ‘not work’ can help giving better answers.

Yes, I fixed the error using the code with NEW as you suggested and it works.
,rows2.Column(“Data”).DateTimeValue.ToString(new Locale(“en-US”), DateTime.FormatStyles.Short, DateTime.FormatStyles.None) but in the display I always get 1/11/24. Can I format it somehow inside this string?

That’s the format the locale use for Short Date style.

If you need something different you need to use ToString and the ICU Date/Time Format mentioned above. ToString("dd-MM-yyyy") or ToString("MM-dd-yyyy") depending on the format you want.

You can add an option to your app for the date format that the user can change if they wish or test the locale.current to select the ToString to use.

Do I necessarily have to assign the date to a variable and then format it?
If so, OK, I’ve already solved it. I wanted to understand if it could be avoided by somehow formatting the DATE data acquired from the database.

stringing together multiple methods calls into a single chain DOES have risks
If the date value is nil then the chain will break with a nil object exception that you need to catch
So sometimes the safer answer is to pull the value into a variable then deal with it with appropriate nil checks etc

Ok.

For instance in this

if rows2.column(“Data”).DateTimeValue returns NIL because the row in the data base has a NULL in it then the attempt to use ToString WILL cause an exception

so be careful with chaining calls together

Tanks.