pjetson Member Licensed User Longtime User Oct 1, 2014 #1 I'm a brand new B4A user, and I'm trying to create a program that calculates the difference between two times. Unfortunately, most likely due to something I'm doing wrong, the difference is 10 hours too high. I've simplified the code down to a single button on the screen that toggles between on and off, and when it toggles off, it displays the elapsed time. If I wait, say, 5 seconds, the time shows as 10:00:05 instead of 00:00:05 Can anyone please help me see what I've done wrong? Code: #Region Project Attributes #ApplicationLabel: Test Program #VersionCode: 1 #VersionName: 'SupportedOrientations possible values: unspecified, landscape or portrait. #SupportedOrientations: unspecified #CanInstallToExternalStorage: False #End Region #Region Activity Attributes #FullScreen: False #IncludeTitle: True #End Region Sub Process_Globals Dim StartTime As Long ' Date/Time that user clicked ON Dim StopTime As Long ' Date/Time that user clicked OFF 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. Private btnStartStop As Button End Sub Sub Activity_Create(FirstTime As Boolean) Activity.LoadLayout("Main") DateTime.DateFormat = DateTime.DeviceDefaultDateFormat btnStartStop.TextColor = Colors.Black btnStartStop.Color = Colors.Green btnStartStop.Text = "ON" End Sub Sub Activity_Resume End Sub Sub Activity_Pause (UserClosed As Boolean) End Sub Sub btnStartStop_Click Dim x As String If btnStartStop.Text = "ON" Then StartTime = DateTime.Now x = "ON at " & DateTime.Date(StartTime) & " " & DateTime.Time(StartTime) ToastMessageShow(x, True) btnStartStop.TextColor = Colors.Black btnStartStop.Color = Colors.Red btnStartStop.Text = "OFF" Else StopTime = DateTime.Now x = "OFF at " & DateTime.Date(StopTime) & " " & DateTime.Time(StopTime) & CRLF & "Duration: " & DateTime.Time(StopTime - StartTime) ToastMessageShow(x, True) btnStartStop.TextColor = Colors.Black btnStartStop.Color = Colors.Green btnStartStop.Text = "ON" End If End Sub Thanks, Peter
I'm a brand new B4A user, and I'm trying to create a program that calculates the difference between two times. Unfortunately, most likely due to something I'm doing wrong, the difference is 10 hours too high. I've simplified the code down to a single button on the screen that toggles between on and off, and when it toggles off, it displays the elapsed time. If I wait, say, 5 seconds, the time shows as 10:00:05 instead of 00:00:05 Can anyone please help me see what I've done wrong? Code: #Region Project Attributes #ApplicationLabel: Test Program #VersionCode: 1 #VersionName: 'SupportedOrientations possible values: unspecified, landscape or portrait. #SupportedOrientations: unspecified #CanInstallToExternalStorage: False #End Region #Region Activity Attributes #FullScreen: False #IncludeTitle: True #End Region Sub Process_Globals Dim StartTime As Long ' Date/Time that user clicked ON Dim StopTime As Long ' Date/Time that user clicked OFF 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. Private btnStartStop As Button End Sub Sub Activity_Create(FirstTime As Boolean) Activity.LoadLayout("Main") DateTime.DateFormat = DateTime.DeviceDefaultDateFormat btnStartStop.TextColor = Colors.Black btnStartStop.Color = Colors.Green btnStartStop.Text = "ON" End Sub Sub Activity_Resume End Sub Sub Activity_Pause (UserClosed As Boolean) End Sub Sub btnStartStop_Click Dim x As String If btnStartStop.Text = "ON" Then StartTime = DateTime.Now x = "ON at " & DateTime.Date(StartTime) & " " & DateTime.Time(StartTime) ToastMessageShow(x, True) btnStartStop.TextColor = Colors.Black btnStartStop.Color = Colors.Red btnStartStop.Text = "OFF" Else StopTime = DateTime.Now x = "OFF at " & DateTime.Date(StopTime) & " " & DateTime.Time(StopTime) & CRLF & "Duration: " & DateTime.Time(StopTime - StartTime) ToastMessageShow(x, True) btnStartStop.TextColor = Colors.Black btnStartStop.Color = Colors.Green btnStartStop.Text = "ON" End If End Sub Thanks, Peter
DonManfred Expert Licensed User Longtime User Oct 1, 2014 #2 Code: Dim p As Period p.Initialize p = DateUtils.PeriodBetween(starttime,stoptime) ' look at log(p.Minutes) ' and the other members... Upvote 0
Code: Dim p As Period p.Initialize p = DateUtils.PeriodBetween(starttime,stoptime) ' look at log(p.Minutes) ' and the other members...
pjetson Member Licensed User Longtime User Oct 1, 2014 #3 Hi, Manfred. Thanks for your reply. I'll try using the DateUtils library to get around the problem. I just realised that my timezone is UTC+10, and the number of hours added is also 10. Coincidence? I just searched the forums again, and found this: http://www.b4x.com/android/forum/threads/periodbetween-string-and-datetime-now.42201/ I'll try setting DateTime.SetTimeZone to see if that fixes the problem too. Thanks, Peter Last edited: Oct 1, 2014 Upvote 0
Hi, Manfred. Thanks for your reply. I'll try using the DateUtils library to get around the problem. I just realised that my timezone is UTC+10, and the number of hours added is also 10. Coincidence? I just searched the forums again, and found this: http://www.b4x.com/android/forum/threads/periodbetween-string-and-datetime-now.42201/ I'll try setting DateTime.SetTimeZone to see if that fixes the problem too. Thanks, Peter
Erel B4X founder Staff member Licensed User Longtime User Oct 1, 2014 #4 pjetson said: I'll try setting DateTime.SetTimeZone to see if that fixes the problem too. Click to expand... It will work. However this is not the correct way to do it. You should not use DateTime.Time for that. You can instead use the code posted here: http://www.b4x.com/android/forum/threads/converting-seconds-in-hh-mm-ss.36264/#post-212804 Upvote 0
pjetson said: I'll try setting DateTime.SetTimeZone to see if that fixes the problem too. Click to expand... It will work. However this is not the correct way to do it. You should not use DateTime.Time for that. You can instead use the code posted here: http://www.b4x.com/android/forum/threads/converting-seconds-in-hh-mm-ss.36264/#post-212804
pjetson Member Licensed User Longtime User Oct 2, 2014 #5 Okay, I'll use p = DateUtils.PeriodBetweenInDays(StartTime,StopTime) to calculate the time difference. However, I need to display it like a time, so what's the absolute simplest way to turn the integer values p.Days, p.Hours, p.Minutes, and p.Seconds into something like d hh:mm:ss? Using NumberFormat() would seem to give me something like (note that I haven't tried this code!): Code: Dim x as String x = NumberFormat(p.Minutes, 2, 0) & ":" & NumberFormat(p.Hours, 2, 0) & ":" & NumberFormat(p.Seconds, 2, 0) If p.Days <> 0 Then x = p.Days & " " & x End If It doesn't seem very efficient to convert integers into doubles and then into strings - is there a better way? Upvote 0
Okay, I'll use p = DateUtils.PeriodBetweenInDays(StartTime,StopTime) to calculate the time difference. However, I need to display it like a time, so what's the absolute simplest way to turn the integer values p.Days, p.Hours, p.Minutes, and p.Seconds into something like d hh:mm:ss? Using NumberFormat() would seem to give me something like (note that I haven't tried this code!): Code: Dim x as String x = NumberFormat(p.Minutes, 2, 0) & ":" & NumberFormat(p.Hours, 2, 0) & ":" & NumberFormat(p.Seconds, 2, 0) If p.Days <> 0 Then x = p.Days & " " & x End If It doesn't seem very efficient to convert integers into doubles and then into strings - is there a better way?
DonManfred Expert Licensed User Longtime User Oct 2, 2014 #6 You can create a small helper-sub for the calculation which returns a string Upvote 0