Android Question Date troubles... please help!

wimpie3

Well-Known Member
Licensed User
Longtime User
OK, let me first get this straight: B4A uses ticks to indicate a date/time. Ticks are the number of milliseconds passed since 1 Jan 1970.

I have a mobile phone where the timezone is set to -9 hours.

When I do this:

B4X:
Dim t As Long=0
Log(DateTime.GetDayOfMonth(t) & " / " & DateTime.GetMonth(t) & " / " & DateTime.GetYear(t))

I do NOT get 1/1/1970, but 31/12/1969. Probably because the timezone comes into play. I don't want that. I want 0 to be 1/1/1970. So I thought, lets set the timezone to 0:

B4X:
DateTime.SetTimeZone(0)
Dim t As Long=0
Log(DateTime.GetDayOfMonth(t) & " / " & DateTime.GetMonth(t) & " / " & DateTime.GetYear(t))

Perfect. 0 ticks is now 1/1/1970. Exactly how I want it.

However, some other date calculations seem to be incorrect now. Just look at the following example. The homescreen of my phone indicates we are the 21st of Feb 2014, 8:18.

B4X:
DateTime.SetTimeZone(0)
Dim t As Long=DateTime.Now
Log(DateTime.GetDayOfMonth(t) & " / " & DateTime.GetMonth(t) & " / " & DateTime.GetYear(t) & " " & DateTime.GetHour(t) & ":" & DateTime.GetMinute(t))
This code returns 21/2/2014 17:18, while I expected (and NEED) it to be 21/2/2014 8:18.

So DateTime.SetTimeZone(0) is not enough to get rid of this whole timezone stuff. Is there something else I should do?
 

derez

Expert
Licensed User
Longtime User


It says that it affects dates !
Try to add the DateTime.GetTimeZoneOffsetAt to t so you'll get the correct ticks.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…