B4J Question date format handle webserver

Roberto P.

Well-Known Member
Licensed User
Longtime User
I noticed that if I set the date format in the main, when you log on to the various handle is used another format.

B4X:
I set, in the main the following format

DateTime.DateFormat    =    "dd/MM/yyyy"
Log("main date format " & DateTime.DateFormat)

for example, if I enter the smyle handle, I see that the formatting is different?!

How can I set a date format that is automatically seen by all handles?

thank you

I attach example.
 

Attachments

  • webserverexample.zip
    66.2 KB · Views: 232

Roycefer

Well-Known Member
Licensed User
Longtime User
Changing the DateFormat is done on a per-thread basis. When you change the DateFormat in your Main module, that is most likely executed on the Main thread and so only applies on the Main thread. You have a couple options: 1) run all your Handlers as single-threaded (they'll run in the Main thread and use the Main thread's DateFormat) or 2) set the DateFormat in each Handler.

Option 2 can be quite simple: just create a Global String in Main called dfString = "dd/MM/yyyy" and do this in the Initialize method of your Handlers (and WebSockets):
B4X:
DateTime.DateFormat = Main.dfString
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
Changing the DateFormat is done on a per-thread basis. When you change the DateFormat in your Main module, that is most likely executed on the Main thread and so only applies on the Main thread. You have a couple options: 1) run all your Handlers as single-threaded (they'll run in the Main thread and use the Main thread's DateFormat) or 2) set the DateFormat in each Handler.

Option 2 can be quite simple: just create a Global String in Main called dfString = "dd/MM/yyyy" and do this in the Initialize method of your Handlers (and WebSockets):
B4X:
DateTime.DateFormat = Main.dfString
I thank you, I would avoid the second option, and find out how to set the date format for all threads without having to set for all handles.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
You can write a simple global method to do the same.. as clever as you want (pass in date, format it and pass back, set it to original format before exiting method)

B4X:
Dim df As String = DateTime.DateFormat  ' save the last known (default) date format
DateTime.DateFormat = "MMM dd, yyyy"
' mod your date vars - then change it back
DateTime.DateFormat = df

df = default format (ie. "yyyy-MM-dd")

At times I need to change it to show full text (January 12, 2017), and then I want to reset it to default (2017-01-12). Set a global as default, and when in doubt if your default is correct, call before setting (recording) and date with DateTime.DateFormat = Main.df...
 
Last edited:
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hi Harris,
thanks you for answer.

It is what I did.

I ask is if there is a way to set my format (Italian = dd / mm / yyyy), without having to set the default for each handle?

Thank you
 
Upvote 0
Top