Time between

bobsimoneau

Member
Licensed User
Longtime User
I am having trouble with something I thought would be easy. I just can not seems to get it. I am trying to create a countdown from now to my wedding on Aug 10th, 2012 5:30 pm. I am looking to get:

Days Hours Mins Secs

My concept was to take the number of ticks between and convert to seconds and then do the math for each. I must be getting old, as this is beating me up. I would appreciate any help.
 

klaus

Expert
Licensed User
Longtime User
Are you sure ?
If the target is tomorrow 5 pm.
Today at 4 pm the difference is 1 day 1 hour
Today at 6 pm the difference is 0 day 23 hours.

Best regards.
 
Upvote 0

bobsimoneau

Member
Licensed User
Longtime User
You are correct, I thought I was losing it. I went to bed last night, after giving up because it was a day off. This morning I did nothing, and it is dead on. I don't understand that.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry, I don't understand.
I went to bed last night, after giving up because it was a day off.
Was it really a day off ?

This morning I did nothing, and it is dead on.
What do you mean with it is dead on.
Is it OK or not ?

Best regards.
 
Upvote 0

bobsimoneau

Member
Licensed User
Longtime User
When run in the morning it works. I just ran it and it says: 77 days, 9 hours, 21 Minutes and 16 seconds. This is correct, as it is 8:09 am Eastern Time in the US. Later on I will try it and it will have 78 days. Happens everytime. I have not been able to figure out exactly when it goes wrong. Thanks for all your help on this.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@Bob and Klaus: I redesigned the app using my code for calculating the date and time to the big day event. I also, added a new layout for a 7 inch tablet. Attched is the zip file of the entire application. I use a timer. If you can improve it, that is great. I tested thoroughly before posting.
Good luck.
 

Attachments

  • EventCounterMahares.zip
    81.9 KB · Views: 168
Upvote 0

bobsimoneau

Member
Licensed User
Longtime User
Your system does work however, the results are to slow to maintain the output in seconds. Sometimes it misses a second being displayed. I wish I could have figured out what was happening with Klaus's procedure as it appears alot faster.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I ran the application on an old 7 inch tablet running OS 2.2 and another one running OS 4.0. Both ran very fast. I had it in debug mode. It will even run faster when in release mode. It is not skipping seconds, but the time updates every 8 seconds on the screen instead of every second, which is insignificant when you are dealing with days, unless of course you are using it to time an olympic 100 meter race. Above all, it yields the correct result.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry, but I tried several times now and I never encountered that there was 1 day more. So I really don't undestand.
What you could do is to ignore the seconds and display only minutes but it's only a workaround.
In my proposal I displayed the seconds to be sure that the program works correctly.
Of course seconds could become important at the very last time, if one of you had some delay, and even though the seconds are not improtant but minutes.
Nevertheless I agree that seeing seconds changing is more 'dynamic'.

Best regards.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@Klaus: In my code, the time updates every 8 seconds, so we are talking about updating about 8 times a minutes, that is more than enough. I do no think anybody is going to be running the app minutes before the start of the ceremony anyway.
On the other hand, I ran the program you created and here is what I found out if this helps you: It runs very well and displays the correct time during the day. As soon as 8 PM Eastern US time hits, the days jump by one day. (isn't 8 pm Eastern 12 midnight UTC????). In other words, if the days are 75, they become 76. Then, in the morning it is good again.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Thanks for the information.
I will try tomorrow and set different times in the Emulator to find the problem.
I tested it only with Central European time on my Nexus One.

Best regards.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Unfortunately I coulndn't yet reproduce this error, so I don't know where and what to look at.
Could you look more closely at what time you get one day more and at what time it becomes 'normal' again.

Best regards.
 
Upvote 0

bobsimoneau

Member
Licensed User
Longtime User
The results are off by a day (returns 1 to many) between 8pm and midnight. I believe it is because I am UTC -4.
 
Last edited:
Upvote 0

bobsimoneau

Member
Licensed User
Longtime User
Now fixed in the latest 2.0 version. Thank you Klaus, Mahares, and Erel. You people help make a GREAT product even better by your willingness to help.
 
Upvote 0

SandroB4A

Member
Licensed User
Longtime User
greetings and infos

Hi to all,

first of all I wish to do my (late) greetings to bobsimoneau for his wedding (were all on time?) and also I take the chance to ask a clarification regarding the following line present on the Klaus example:

DateTicks = DateTime.DateParse(Date) + (DateTime.TimeParse(Time) - DateTime.DateParse(DateTime.Date(DateTime.Now)))

why not just
DateTicks = DateTime.DateParse(Date) + (DateTime.TimeParse(Time) - DateTime.Now ?

Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Starting with v2.50 there is a new method that simplifies these calculations: DateTimeParse.

B4X:
Sub DateBetweenTwoDates(D1 As String, T1 As String, D2 As String, T2 As String)
    Dim start, endTime, t As Long
    start = DateTime.DateTimeParse(D1, T1)
    endTime = DateTime.DateTimeParse(D2, T2)
    Dim days, hours, minutes, seconds As Int
    t = Abs(endTime - start)
    days = Floor(t / DateTime.TicksPerDay)
    hours = Floor((t Mod DateTime.TicksPerDay) / DateTime.TicksPerHour)
    minutes = Floor((t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute)
    seconds = Floor((t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond)
    
    Log("Days = " & days)
    Log("Hours = " & hours)
    Log("Minutes = " & minutes)
    Log("Seconds = " & seconds)
    Msgbox("Days = " & days & CRLF & "Hours = " & hours & CRLF & "Minutes = " & minutes & CRLF & "Seconds = " & seconds, "OK")
    
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Explanations for:
B4X:
DateTicks = DateTime.DateParse(Date) + (DateTime.TimeParse(Time) - DateTime.DateParse(DateTime.Date(DateTime.Now)))
DateTime.TimeParse(Time) returns the ticks of today + the ticks of the given time!
DateTime.DateParse(DateTime.Date(DateTime.Now)) returns the ticks for today at 0h00 !
DateTime.TimeParse(Time) - DateTime.DateParse(DateTime.Date(DateTime.Now)) returns only the ticks for the given time.

DateTime.Now returns the ticks of today + the ticks of the current time.

Best regards.
 
Upvote 0
Top