Time problems again

hackhack

Active Member
Licensed User
Longtime User
Ok, I'm back to my time problem.

I have current time in T1 (long) - then using datetime.add I arrived at a date in the future called T2 (usually less than a week)

And so I assumed doing T2-T1=time to T2

However as we found out a while ago, there is some sort of adjustment being done to the time so its always a few hours off - apparently this is related to time zone settings (and something everybody else understands but me)

However the question is, how would i go about calculating the amount of from Point in time1 to Point in time2 ?
 

hackhack

Active Member
Licensed User
Longtime User
Well something like this (not exact code)

B4X:
DateTime.DateFormat= "dd/MM/yyyy HH:mm:ss"
DateTime.TimeFormat= "HH:mm:ss"
   
dim t1 as long
t1=DateTime.Now

dim a as string
a="03/09/2011 18:00:00"

dim t2 as long
t2=DateTime.DateParse(a)

t2=t2-t1

dim s as string
   s=NumberFormat2(DateTime.GetDayOfMonth(t2),2,0,0,False) & ":" _
    & NumberFormat2(DateTime.GetHour(t2),2,0,0,False) & ":" _
      & NumberFormat2(DateTime.GetMinute(t2),2,0,0,False) & ":" _
         & NumberFormat2(DateTime.GetSecond(t2),2,0,0,False)

And in s the hour component is off by an hour or two.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
You are right. It is an hour off for me.
Looks like it is returning difference in GMT and currently we are at BST.

I think the answer is the get your Timezone difference from GMT and add it to the difference.

Just to elaborate if you still dont understand it.
t1 is the current time in your phone
t2 is defined as a string. This will be taken as an absolute time value i.e. with respect to UTC or GMT. So this time is NOT in the same timezone as you are.
So when you do the difference you need to offset it with the same value. This can be done as Erel suggested somewhere with
timezone = -DateTime.DateParse("01/01/1970")
This will negate the effect of UTC and bring you back into your own timezone.

I find it much easier to deal with relative time, rather than defining an absolute time somewhere.

EDIT: Hold on, that didnt work for me.
 
Last edited:
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
This seems to works:

B4X:
DateTime.DateFormat= "dd/MM/yyyy HH:mm:ss"
DateTime.TimeFormat= "HH:mm:ss"

Dim timezone As Long
timezone = -DateTime.DateParse("01/01/1970 00:00:00")
   
Dim t1 As Long
t1=DateTime.Now

Dim a As String
a="03/09/2011 02:33:00"

Dim t2 As Long
t2=DateTime.DateParse(a)

t2=t2-t1-timezone   '<----!!!

Dim s As String
   s=NumberFormat2(DateTime.GetDayOfMonth(t2),2,0,0,False) & ":" _
    & NumberFormat2(DateTime.GetHour(t2),2,0,0,False) & ":" _
      & NumberFormat2(DateTime.GetMinute(t2),2,0,0,False) & ":" _
         & NumberFormat2(DateTime.GetSecond(t2),2,0,0,False)
Msgbox (s,"")
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
Just to elaborate if you still dont understand it.
t1 is the current time in your phone
t2 is defined as a string. This will be taken as an absolute time value i.e. with respect to UTC or GMT. So this time is NOT in the same timezone as you are.

Ah, ok, that makes sense.



This seems to works:

B4X:
t2=t2-t1-timezone   '<----!!!

Hm, but how do I calculate that?

I mean I can hardcode a timezone offset, but better if I could read that, and also take into account daylight savings.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Ah, ok, that makes sense.





Hm, but how do I calculate that?

I mean I can hardcode a timezone offset, but better if I could read that, and also take into account daylight savings.
When you do:
timezone = -DateTime.DateParse("01/01/1970 00:00:00")
It will give you the difference of UTC/GMT with your current timezone.
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
Heh, I'm getting an "unparseable date: 01/01/1970" error, overlooked you said to add "00:00:00" :)
 
Last edited:
Upvote 0

DKnowles

Member
Licensed User
Longtime User
Thanks for the details.

Thanks for this, I had the opposite effect as i was sending the time of a reading to the server but the time was GMT not BST (in UK).

To correct for this just ADD the timezone value.

Cheers
 
Upvote 0

KashMalaga

Member
Licensed User
Longtime User
Im comparing two dates. Today and enddate for example: 20111130.

as you can imagine the problem comes days like today 2011113

The 0 missing gives troubles. must be like that 20111103

To work with:

If DateTime.DateParse(endate) < DateTime.DateParse(todaydate) Then

Some ideas?

Thanks!
 
Upvote 0
Top