Android Question Difference Between Two Times one in PM and on other in AM

Alhootti

Member
I have two times
10:30 PM ----- > start time
05:30 AM ----- >end time

The result should be 07:00

How to calculate this ?
 

Mahares

Expert
Licensed User
Longtime User
I have two times
10:30 PM ----- > start time
05:30 AM ----- >end time
You must involve the date since your times appear to be in different dates. Here is the full code:
B4X:
Log(HoursMinutesBetween("12/23/2022 10:30 pm", "12/24/2022 05:30 am"))   'displays: 07:00
B4X:
Sub HoursMinutesBetween(Startime As String, Endtime As String) As String
    Dim diff1 As Period
    DateTime.DateFormat= "MM/dd/yyyy hh:mm a" 'you must involve the date also. You can use a different date format
    diff1 = DateUtils.PeriodBetween(DateTime.DateParse(Startime), DateTime.DateParse(Endtime) )
    Return $"${NumberFormat(diff1.Hours,2,0)}:${NumberFormat(diff1.minutes,2,0)}"$
End Sub
Please do not use bold text
 
Upvote 1

Alhootti

Member
You must involve the date since your times appear to be in different dates. Here is the full code:
B4X:
Log(HoursMinutesBetween("12/23/2022 10:30 pm", "12/24/2022 05:30 am"))   'displays: 07:00
B4X:
Sub HoursMinutesBetween(Startime As String, Endtime As String) As String
    Dim diff1 As Period
    DateTime.DateFormat= "MM/dd/yyyy hh:mm a" 'you must involve the date also. You can use a different date format
    diff1 = DateUtils.PeriodBetween(DateTime.DateParse(Startime), DateTime.DateParse(Endtime) )
    Return $"${NumberFormat(diff1.Hours,2,0)}:${NumberFormat(diff1.minutes,2,0)}"$
End Sub
Please do not use bold text
Thank you
It is working properly now
 
Upvote 0
Top