Android Question Time Difference

Discussion in 'Android Questions' started by deyan666, Feb 7, 2015.

  1. deyan666

    deyan666 Member Licensed 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?
     
  2. derez

    derez Expert Licensed 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)
     
    lemonisdead and DonManfred like this.
  3. DonManfred

    DonManfred Expert Licensed User

    Code:
    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:
    Code:
    Dim b = DateTime.Time(EditText3.Text)
    is wrong... You need to use EditText2.Text here
     
    mangojack and lemonisdead like this.
  4. derez

    derez Expert Licensed User

    Correction to my previous answer (I forgot about time offset)
    Code:
    Edittext3.text = DateTime.Time(b-a - DateTime.TimeZoneOffset * DateTime.TicksPerHour)
    You don't need dateutils for this.
     
    DonManfred likes this.
  5. Erel

    Erel Administrator Staff Member Licensed User

    The only correct way to do it is with DateUtils as in @DonManfred code.
     
    DonManfred likes this.
  6. deyan666

    deyan666 Member Licensed 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?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Dim minutes As Int = p.Hours * 60 + p.Minutes
     
  8. DonManfred

    DonManfred Expert Licensed User

    Code:
    Dim worktime As Float = p.hours+(p.minutes/60)
        EditText3.Text = worktime
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice