P Pantelis Member Licensed User Longtime User Jul 16, 2013 #1 SetTimeZone(0) before every DateTime method? Hi all I am trying this code and the result of c is not 01:00:00 if the timezone of my device is not 0. Code: Dim a As Long Dim b As Long Dim c As Long a = DateTime.Now b = a - DateTime.TicksPerHour Log (DateTime.Time(a)) Log (DateTime.Time(b)) c = a - b Log (DateTime.Time(c)) I order to have correct result i change the code like this: Code: Dim a As Long Dim b As Long Dim c As Long Dim Timezone As Double Timezone = DateTime.GetTimeZoneOffsetAt(DateTime.Now) a = DateTime.Now b = a - DateTime.TicksPerHour DateTime.SetTimeZone(0) Log (DateTime.Time(a)) Log (DateTime.Time(b)) c = a - b Log (DateTime.Time(c)) DateTime.SetTimeZone(Timezone) Is this correct way? In what methods of the DateTime And the DateUtils this trick must be done in order to have correct results from them? What methods are affected by the timezone? Last edited: Jul 17, 2013
SetTimeZone(0) before every DateTime method? Hi all I am trying this code and the result of c is not 01:00:00 if the timezone of my device is not 0. Code: Dim a As Long Dim b As Long Dim c As Long a = DateTime.Now b = a - DateTime.TicksPerHour Log (DateTime.Time(a)) Log (DateTime.Time(b)) c = a - b Log (DateTime.Time(c)) I order to have correct result i change the code like this: Code: Dim a As Long Dim b As Long Dim c As Long Dim Timezone As Double Timezone = DateTime.GetTimeZoneOffsetAt(DateTime.Now) a = DateTime.Now b = a - DateTime.TicksPerHour DateTime.SetTimeZone(0) Log (DateTime.Time(a)) Log (DateTime.Time(b)) c = a - b Log (DateTime.Time(c)) DateTime.SetTimeZone(Timezone) Is this correct way? In what methods of the DateTime And the DateUtils this trick must be done in order to have correct results from them? What methods are affected by the timezone?
Erel B4X founder Staff member Licensed User Longtime User Jul 17, 2013 #2 There is no need to set the time zone. What you are doing is incorrect. There isn't any meaning to DateTime.Time(a - b). (a - b) doesn't represent the time instance that you are thinking of. You should instead use DateUtils.PeriodBetween. Upvote 0
There is no need to set the time zone. What you are doing is incorrect. There isn't any meaning to DateTime.Time(a - b). (a - b) doesn't represent the time instance that you are thinking of. You should instead use DateUtils.PeriodBetween.
P Pantelis Member Licensed User Longtime User Jul 17, 2013 #3 Thanks Erel. It was a bad example. I read on forum that in order to have corrrect result with date and time calculations we must set timezone to 0. I try to see if this happens in any of datetime method and as i see i have wrong results from alot of them if timezone in not 0. So i set always the timezone to 0. The problem is that if i do that the DateTime.Now gives me UTC values. How to use the DateTime.Now to get the real date-time value depending on device's settings TimeZone and DST? Upvote 0
Thanks Erel. It was a bad example. I read on forum that in order to have corrrect result with date and time calculations we must set timezone to 0. I try to see if this happens in any of datetime method and as i see i have wrong results from alot of them if timezone in not 0. So i set always the timezone to 0. The problem is that if i do that the DateTime.Now gives me UTC values. How to use the DateTime.Now to get the real date-time value depending on device's settings TimeZone and DST?
Erel B4X founder Staff member Licensed User Longtime User Jul 18, 2013 #4 You should not set the time zone to 0. Use DateUtils and everything will be fine. Upvote 0
P Pantelis Member Licensed User Longtime User Jul 18, 2013 #5 I want to strip time off a date. I found this http://www.b4x.com/android/forum/threads/strip-time-off-a-date.15847/#post-90084 but it requires the TimeZone to be set to 0, otherwise it gives wrong results. Hot to do it with DateUtils? Last edited: Jul 18, 2013 Upvote 0
I want to strip time off a date. I found this http://www.b4x.com/android/forum/threads/strip-time-off-a-date.15847/#post-90084 but it requires the TimeZone to be set to 0, otherwise it gives wrong results. Hot to do it with DateUtils?
Erel B4X founder Staff member Licensed User Longtime User Jul 18, 2013 #6 This thread was created before DateUtils was available. The correct code is: Code: Sub StripTime(Ticks As Long) As Long Return DateUtils.SetDateAndTime(DateTime.GetYear(Ticks), DateTime.GetMonth(Ticks), DateTime.GetDayOfMonth(Ticks), 0, 0, 0) End Sub Upvote 0
This thread was created before DateUtils was available. The correct code is: Code: Sub StripTime(Ticks As Long) As Long Return DateUtils.SetDateAndTime(DateTime.GetYear(Ticks), DateTime.GetMonth(Ticks), DateTime.GetDayOfMonth(Ticks), 0, 0, 0) End Sub
P Pantelis Member Licensed User Longtime User Jul 18, 2013 #7 Great. Thank you very much Erel. Upvote 0
P Pantelis Member Licensed User Longtime User Jul 19, 2013 #8 Erel i try To understand why the old method while looks ok gives wrong result and found this. I set the device with this settings: Device settings: date: 01/Jan/2013 (No DST on this time) TimeZone: +3 Time: 10:00:00 AM (Offset from UTC: +3) Code: Dim InputDate, OnlyTime As Long InputDate = DateTime.Now OnlyTime = InputDate Mod DateTime.TicksPerDay Log(DateTime.time(OnlyTime)) 'Original time 10:00:00 AM and Returns 10:00:00 AM. OK. Log(DateTime.GetHour(OnlyTime)) '10 Then i change the settings of device with: Device settings: date: 01/Jul/2013 (We have DST on this time) TimeZone: +4 Time: 10:00:00 AM (Offset from UTC: +4, (timezone +3 and DST +1)) and try the same code Code: Dim InputDate, OnlyTime As Long InputDate = DateTime.Now OnlyTime = InputDate Mod DateTime.TicksPerDay Log(DateTime.time(OnlyTime)) 'Original time 10:00:00 AM and Returns 09:00:00 AM. Log(DateTime.GetHour(OnlyTime)) '9 DateTime.Time ignores the DST? The same happend with DateTime.Hour Last edited: Jul 19, 2013 Upvote 0
Erel i try To understand why the old method while looks ok gives wrong result and found this. I set the device with this settings: Device settings: date: 01/Jan/2013 (No DST on this time) TimeZone: +3 Time: 10:00:00 AM (Offset from UTC: +3) Code: Dim InputDate, OnlyTime As Long InputDate = DateTime.Now OnlyTime = InputDate Mod DateTime.TicksPerDay Log(DateTime.time(OnlyTime)) 'Original time 10:00:00 AM and Returns 10:00:00 AM. OK. Log(DateTime.GetHour(OnlyTime)) '10 Then i change the settings of device with: Device settings: date: 01/Jul/2013 (We have DST on this time) TimeZone: +4 Time: 10:00:00 AM (Offset from UTC: +4, (timezone +3 and DST +1)) and try the same code Code: Dim InputDate, OnlyTime As Long InputDate = DateTime.Now OnlyTime = InputDate Mod DateTime.TicksPerDay Log(DateTime.time(OnlyTime)) 'Original time 10:00:00 AM and Returns 09:00:00 AM. Log(DateTime.GetHour(OnlyTime)) '9 DateTime.Time ignores the DST? The same happend with DateTime.Hour
Erel B4X founder Staff member Licensed User Longtime User Jul 21, 2013 #9 Check DateTime.Date(OnlyTime). You will see why the DST is "ignored". Upvote 0
P Pantelis Member Licensed User Longtime User Jul 21, 2013 #10 Of course. There is no DST in 1/1/1970. Thank you very much Erel. Upvote 0
LucaMs Expert Licensed User Longtime User Apr 21, 2014 #11 I have the same problem (#1). I can not use DateUtils because I need milliseconds. (Why if I use: Code: DateTime.TimeFormat = "HH:mm:ss:SSS" Dim NowStart as long NowStart = DateTime.Now ' ... Label1.Text = DateTime.Time(DateTime.Now - NowStart) 'It returns +1 hour it does not work?) The problem can not be related to "time zone" or other settings, since both the initial and final time are used with the same settings. If DateTime.Now is a Long value in milliseconds, the difference should be ... the difference in milliseconds! Last edited: Apr 21, 2014 Upvote 0
I have the same problem (#1). I can not use DateUtils because I need milliseconds. (Why if I use: Code: DateTime.TimeFormat = "HH:mm:ss:SSS" Dim NowStart as long NowStart = DateTime.Now ' ... Label1.Text = DateTime.Time(DateTime.Now - NowStart) 'It returns +1 hour it does not work?) The problem can not be related to "time zone" or other settings, since both the initial and final time are used with the same settings. If DateTime.Now is a Long value in milliseconds, the difference should be ... the difference in milliseconds!
Erel B4X founder Staff member Licensed User Longtime User Apr 21, 2014 #12 There are several problems in your code. As a general rule date and time arithmetics are much more complicated than it may seem and should be avoided. Please start a new thread and explain the problem you are trying to solve. Upvote 0
There are several problems in your code. As a general rule date and time arithmetics are much more complicated than it may seem and should be avoided. Please start a new thread and explain the problem you are trying to solve.