iOS Question DateUtils SetDate - Cannot parse: invalid date

Alexander Stolte

Expert
Licensed User
Longtime User
I'm using the following code to set the DateTime locale to the system preferred language.
B4X:
Sub SetDateTimeLocale (locale As String)
    Dim loc2 As NativeObject
    loc2 = loc2.Initialize("NSLocale").RunMethod("localeWithLocaleIdentifier:", Array(locale))
    Dim no As NativeObject = DateTime
    no.GetField("dateFormat").SetField("locale", loc2)
    no.GetField("timeFormat").SetField("locale", loc2)
End Sub

Sub GetPreferredLanguage As String
    Dim no As NativeObject
    Return no.Initialize("NSLocale").RunMethod("preferredLanguages", Null).RunMethod("objectAtIndex:", Array(0)).AsString
End Sub
if i do this and call the DateUtils.SetDate function, the following error occurs:
B4X:
Application_Start
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
Device locale: de
Found 75 strings.
Application_Active
Error: Invalid value: AD20230101 000000
Error occurred on line: 437 (AS_DatePicker)
Cannot parse: invalid date
Stack Trace: (
  CoreFoundation       __exceptionPreprocess + 242
  libobjc.A.dylib      objc_exception_throw + 48
  CoreFoundation       -[NSException initWithCoder:] + 0
  3Things              -[B4I ObjectToNumber:] + 386
  3Things              -[b4i_dateutils _setdateandtime::::::] + 3079
  3Things              -[b4i_dateutils _setdate:::] + 81
  3Things              -[ResumableSub_as_datepicker_CreateMonthView resume::] + 608
  3Things              -[b4i_as_datepicker _createmonthview:] + 314
  3Things              -[b4i_as_datepicker _designercreateview::::] + 2660
  CoreFoundation       __invoking___ + 140
 CoreFoundation       -[NSInvocation invoke] + 302
 3Things              +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1519
 3Things              -[B4IShell runMethod:] + 384
 3Things              -[B4IShell raiseEventImpl:method:args::] + 2098
 3Things              -[B4IShellBI raiseEvent:event:params:] + 1354
 3Things              -[B4ICustomViewWrapper AfterDesignerScript] + 1658
 3Things              -[B4ILayoutBuilder loadLayout:::] + 2835
 3Things              -[B4IPanelWrapper LoadLayout::] + 195
 3Things              -[B4XViewWrapper LoadLayout::] + 95
 3Things              -[b4i_b4xp_task _xtf_date_buttonclick:] + 966
 CoreFoundation       __invoking___ + 140
 CoreFoundation       -[NSInvocation invoke] + 302
 3Things              +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1519
 3Things              -[B4IShell runMethod:] + 384
 3Things              -[B4IShell raiseEventImpl:method:args::] + 2098
 3Things              -[B4IShellBI raiseEvent:event:params:] + 1354
 3Things              -[B4ICommon CallSub4::::] + 286
 3Things              -[B4ICommon CallSub:::] + 145
 3Things              -[b4i_as_textfieldadvanced _buttonclick:] + 772
 3Things              -[b4i_as_textfieldadvanced _xlbl_buttontext_click:] + 301
 CoreFoundation       __invoking___ + 140
 CoreFoundation       -[NSInvocation invoke] + 302
 3Things              +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1519
 3Things              -[B4IShell runMethod:] + 384
 3Things              -[B4IShell raiseEventImpl:method:args::] + 2098
 3Things              -[B4IShellBI raiseEvent:event:params:] + 1354
 3Things              __33-[B4I raiseUIEvent:event:params:]_block_invoke + 51
 libdispatch.dylib    _dispatch_call_block_and_release + 12
 libdispatch.dylib    _dispatch_client_callout + 8
 libdispatch.dylib    _dispatch_main_queue_drain + 1420
 libdispatch.dylib    _dispatch_main_queue_callback_4CF + 31
 CoreFoundation       __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
 CoreFoundation       __CFRunLoopRun + 2463
 CoreFoundation       CFRunLoopRunSpecific + 557
 GraphicsServices     GSEventRunModal + 137
 UIKitCore            -[UIApplication _run] + 972
 UIKitCore            UIApplicationMain + 123
 3Things              main + 104
 dyld                 start_sim + 10
 ???                  0x0 + 8675672966
)
Error: Invalid value: AD20230101 000000
 
Solution
Change the code to:
B4X:
Sub SetDateTimeLocale (locale As String)
    Dim loc2 As NativeObject
    loc2 = loc2.Initialize("NSLocale").RunMethod("localeWithLocaleIdentifier:", Array(locale))
    Dim no As NativeObject = DateTime
    no.GetField("dateFormat").SetField("locale", loc2)
    no.GetField("timeFormat").SetField("locale", loc2)
    Me.As(NativeObject).GetField("_dateutils").SetField("_ad", "")
End Sub

It can fail in debug mode in some edge cases. Clean the project and it will then work.

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change the code to:
B4X:
Sub SetDateTimeLocale (locale As String)
    Dim loc2 As NativeObject
    loc2 = loc2.Initialize("NSLocale").RunMethod("localeWithLocaleIdentifier:", Array(locale))
    Dim no As NativeObject = DateTime
    no.GetField("dateFormat").SetField("locale", loc2)
    no.GetField("timeFormat").SetField("locale", loc2)
    Me.As(NativeObject).GetField("_dateutils").SetField("_ad", "")
End Sub

It can fail in debug mode in some edge cases. Clean the project and it will then work.
 
Upvote 0
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…