Datetime problem or is it my code?


Licensed User
Longtime User
I have a simple script like below:

s = "11/26/1939"
' not sure how to parse the string, so using manual method for now.
dD = s.SubString2(3,5)
dM = s.SubString2(0,2)
dY = s.SubString(6)
' anyway, as i'm using dd/mm/yy, I need to reset it
DateTime.DateFormat = "MM/dd/yyyy"
dt = DateTime.DateParse(dM & "/" & dD & "/" & dY)
' set the date as I want to have the day string as well
DateTime.DateFormat = "d MMM yyyy, EEEE"
s2 = DateTime.Date(dt)
Label2.Text = "BirthDate: " & s2.ToUpperCase

The result is shown as: Birthdate: 25 NOV 1939, SATURDAY
When it should be: Birthdate: 26 NOV 1939, SUNDAY

I then did some sample script using DateDialog:

Dim Dd As DateDialog
DD.DayOfMonth = 26
DD.Month = 11
DD.Year = 1939
dt = dd.DateTicks
s2 = DateTime.Date(dt)

The result is the same - it's showing 25 Nov 1939

And now... further observations:

The date works correctly in emulator but not in the device.
Most of the time, it's a day earlier. I also noticed that if the year is 18xx, it is OK. But when it's 19xx, it's not.

Have anyone encounter similar problem with the above code in your device or emulator?

WZ Sun


B4X founder
Staff member
Licensed User
Longtime User
I'm getting the expected behavior on several devices.
I believe that it is related to a different locale.

Try to explicitly define the time zone to your phone time zone when parsing:
Sub Activity_Create(FirstTime As Boolean)
    Dim t As Long
    DateTime.DateFormat = "MM/dd/yyyy z"
    t = DateTime.DateParse("11/26/1939 +0800") 'GMT +8
    DateTime.DateFormat = "d MMM yyyy, EEEE"
    Msgbox(DateTime.Date(t), DateTime.Time(t))
End Sub


Licensed User
Longtime User
Hi Erel,
Thanks. I tried... it doesn't work. Anyway, I'll do some workaround.

WZ Sun