Android Question Time Difference

deyan666

Member
Licensed User
Longtime User
can anyone help me !!!

Just need to find difference between two times but i think im over complicating it.

i want the edittext field to be the input

and i want the answer to be shown in an edit text.

(for example edit text1 is 0700 and edit text2 is 1500 how can i calculate difference between these two)?

for eg:


DateTime.TimeFormat="HHmm"
Dim a = DateTime.Time(EditText1.Text)
Dim b = DateTime.Time(EditText3.Text)
EditText3 = b - a

why doesnt this work?
 

derez

Expert
Licensed User
Longtime User
You need to use
Dim a as long = DateTime.TimeParse(EditText1.Text)
Dim b as long = DateTime.TimeParse(EditText3.Text)
With this method you get ticks, so after calculating the difference b-a you need to convert it to time :
EditText3 = DateTime.Time(b-a)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim a As Long = DateTime.TimeParse(edittext1.Text)
    Dim b As Long = DateTime.TimeParse(edittext2.Text)
    Dim p As Period = DateUtils.PeriodBetween(a,b)
   
    EditText3.Text = p.Hours&" hours, "&p.Minutes&" minutes"

btw:
B4X:
Dim b = DateTime.Time(EditText3.Text)
is wrong... You need to use EditText2.Text here
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Correction to my previous answer (I forgot about time offset)
B4X:
Edittext3.text = DateTime.Time(b-a - DateTime.TimeZoneOffset * DateTime.TicksPerHour)
You don't need dateutils for this.
 
Upvote 0

deyan666

Member
Licensed User
Longtime User
Yeap. i Tried them both out and Don Manfred worked out well. One question. how can the answer come back as a float number. Example 0900- 1730 = 8.30

I need these floats to multiply with a standard rate of pay. Any Suggesitons?
 
Upvote 0
Top