DeviceDefaultDateFormat and initial DateFormat don't match locale

AllanH

Member
Licensed User
Longtime User
Erel

Great app software!
My app requires to convert dates of birth to age but I am struggling to parse the date of birth. It appears that the DateParse function is getting confused and I am also confused by the local formats.
In a UK locale with the settings of dd/mm/yyyy in the simulator,

DeviceDefaultDateFormat = d MMM yyyy
DateTime.DateFormat = MM/dd/yyyy

On my phone, set to UK and Date format of Day/MonthYear, I get:

DeviceDefaultDateFormat = MMM d,y
DateTime.DateFormat = MM/dd/yyyy

I really don't want to have to put code into my app to ask the user how they write their dates since I should be able to get this from the system. Can you explain why the default and the initial DateFormat are not the same and why neither correspond with the system settings?

Cheers

Allan Harkness
 

AllanH

Member
Licensed User
Longtime User
Erel

Thanks for the prompt reply
However, P.GetSettings("date_format") returns "dd-MM-yyyy"
The "-" is not a standard UK date separator so I now need to cope with parsing this to an appropriate locale specific date separator (I will probably use whatever the user has typed in!).

The DeviceDefaultDateFormat seems to be returning the long date format - any way of getting the short date format, that might even use "/"?

Allan
 

AllanH

Member
Licensed User
Longtime User
Sure - my code now works but it requires the user to enter a date before I can find out what his local date separator is.

For a UK app, I could just convert "-" always to "/" but then the app will fail in a country that doesn't use "/"

I'd like to prompt the user to type in the date with a 4 digit year (eg a hint that the expected format is "dd/mm/yyyy", or their equivalent in a non-UK country). However, I can't prompt them till I know what their date separator is! NB I know "mm" is minutes in B4A but end-users know what I mean.

My phone displays short dates as "dd/MM/yyyy" and so does my Window's applications. So how come I can't get that format string directly from any B4A system setting?

Allan
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
P.GetSettings("date_format") returns the stored settings.

Theoretically you should just pass this value to DateFormat.
This code is based on a different implementation and it will always return the separator as '/'.
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log(GetDeviceDateFormatSettings)
End Sub

Sub GetDeviceDateFormatSettings As String
   Dim r As Reflector
   r.Target = r.RunStaticMethod("android.text.format.DateFormat", "getDateFormat", Array As Object(r.GetContext), _
      Array As String("android.content.Context"))
   Return r.RunMethod("toPattern")
End Sub

You can also show the user an example next to the input field.
 
Top