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.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Mazal tov!

Here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim target As Long
   target = DateTime.DateParse("08/10/2012")
   target = target + 17 * DateTime.TicksPerHour + 30 * DateTime.TicksPerMinute
   Log(ConvertTicksToTimeString(target - DateTime.Now))
End Sub



Sub ConvertTicksToTimeString(t As Long) As String
    Dim days, hours, minutes, seconds As Int
   days = t / DateTime.TicksPerDay
    hours = (t Mod DateTime.TicksPerDay) / DateTime.TicksPerHour
    minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
    seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
    Return days & " days, " & NumberFormat(hours, 2, 0) & " hours, " _
      & NumberFormat(minutes, 2, 0) & " minutes and " & NumberFormat(seconds, 2, 0) & " seconds"
End Sub

(double posted with Klaus)
 
Upvote 0

bobsimoneau

Member
Licensed User
Longtime User
Both examples appear to work, but are off by 4 hours. Is this because I am UTC +5 Eastern zone. Thanks for the assistance.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Hre is another way that works for me exactly, but uses SQL. This is a testament to the flexibility of B4A:
B4X:
       Dim objSql As SQL
            Dim DateTimeFormatOrig,txtDateNow, txtTimeNow As String
         DateTimeFormatOrig=DateTime.DateFormat
         DateTime.DateFormat = "yyyy-MM-dd"      
         DateTime.TimeFormat = "HH:mm:ss" 
         Dim txt As String
         Dim NDays, Nhours,Nmins,Nsecs As Float
         Dim Days,Hours,Mins,Secs As Int
         
         txtDateNow=DateTime.Date(DateTime.Now)  :txtTimeNow=DateTime.Time(DateTime.Now)
         objSql.Initialize("","",False) 'Create and initialize a dummy database
         txt = "SELECT julianday('2012-08-10 17:30:00') - julianday('" & txtDateNow & " " & txtTimeNow & "')"
         NDays = objSql.ExecQuerySingleResult(txt)      
         Days=Floor(NDays)
         Nhours=(NDays-Days) * 24  :Hours=Floor(Nhours)
         Nmins=(Nhours-Hours)*60   :Mins=Floor(Nmins)
         Nsecs=(Nmins-Mins)*60      
         Msgbox(Days & " days " & Hours & " hrs " & Mins & " min " & Secs & " sec to the big day."  ,"Congratulations.")
         objSql.Close
         DateTime.DateFormat=DateTimeFormatOrig 'go back to orig format
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Are you sure that the device or emulator is set to the timezone you are refering to?
I changed the timezone on my device from Central European Time to New York time and the code works OK.
Then I defined a new Emulator without setting the timezone and the result is 4 hours off with New York time !?

Best regards.
 
Upvote 0

bobsimoneau

Member
Licensed User
Longtime User
I apolize, the emulator was off by 4 hours. I will have to figure out how to fix the emulator so as not to have to set it each time. Every offering worked. Thank you all.
 
Upvote 0

bobsimoneau

Member
Licensed User
Longtime User
I thought I had it figured out, but still no good. I took Klaus's example which ran and modified to add labels. I can not get the labels to update.

'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim TimeTarget As Long
Dim Timer1 As Timer
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim Label1 As Label
Dim Label2 As Label
Dim Label3 As Label
Dim Label4 As Label
Dim Label5 As Label
Dim lblDays As Label
Dim lblHrs As Label
Dim lblMins As Label
Dim lblSecs As Label
Dim Label10 As Label
Dim Label11 As Label
Dim Label6 As Label
Dim Label7 As Label
Dim Label9 As Label
Dim ImageView1 As ImageView
Dim Label12 As Label
Dim Label13 As Label
Dim Label8 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim Time As String
Activity.LoadLayout("Main")
DateTime.DateFormat = "MM/dd/yyyy"
DateTime.TimeFormat = "hh:mm aa"
TimeTarget = GetTicks("08/10/2012", "5:30 pm")

Timer1.Initialize("Timer1", 1000)
Timer1.Enabled = True
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Timer1_Tick
updateTime(TimeTarget)
End Sub

Sub GetTicks(Date As String, Time As String) As Long
Dim DateTicks As Long

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

Return DateTicks
End Sub

Sub updateTime(Target As Long)
Dim Days, Hours, Minutes, Seconds As Int
Dim TimeDiff As Long

TimeDiff = Target - DateTime.Now
Days = Floor(TimeDiff / DateTime.TicksPerDay)

TimeDiff = (TimeDiff Mod DateTime.TicksPerDay)' * DateTime.TicksPerHour
Hours = Floor(TimeDiff / DateTime.TicksPerHour)

TimeDiff = (TimeDiff Mod DateTime.TicksPerHour)' * DateTime.TicksPerMinute
Minutes = Floor(TimeDiff / DateTime.TicksPerMinute)

TimeDiff = (TimeDiff Mod DateTime.TicksPerMinute)' * DateTime.TicksPerSecond
Seconds = Floor(TimeDiff / DateTime.TicksPerSecond)

lblDays.Text = Days
lblHrs.Text = Hours
lblMins.Text = Minutes
lblSecs.Text = Seconds
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Hi Bob,
Here you are.
You were mixing up your Labels.
In your program you have Label10 on top of lblSecs hiding it.
There some Labels not used I'v removed them.

Best regards and all my wishes for this great day.
 

Attachments

  • EventCounter1.zip
    81.4 KB · Views: 233
Upvote 0

Mahares

Expert
Licensed User
Longtime User
No. It is not off by one day. It is right on the money. I tested the code in the sample provided to you by Klaus and also the code sample I sent you which uses the Julian method. Both yield ther exact same date and time to the second. You must have the jitters of the big day.

See next post. Bob, yo are correct!
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Bob, you are absolutely correct. the code you received from Klaus is 1 day off. I tested the code in the morning of May 24th. It showed the correct number of days and hours and minutes. But, strangely enough, I tested it at night around 10 pm and it showed 78 days, x hrs, y mns, z sec instead of 77days , x hrs, y mns, z sec. Very strange, correct in the morning and wrong at night. I think it is back to the drawing board for Klaus.
I tested my own code that I posted for you that uses the Julian method and it is correct all the time. You may want to check it out for yourself.
 
Upvote 0
Top