Android Question DateUtils Calculate interval days in hours

Discussion in 'Android Questions' started by Roberto P., Jul 29, 2015.

  1. Roberto P.

    Roberto P. Well-Known Member Licensed User

    I'm using the management DateUtils.PeriodBetween to calculate the interval in hours between two dates. Shall not count the hours when you exceed one day, for example:

    07/29/2015 - 10:00
    30/07/2015 - 11:00

    They are calculated 1 hour but not 25 hours

    there is a way to properly calculate elapsed time?
    thank you
  2. DonManfred

    DonManfred Expert Licensed User

    25 hour is the proper value!
    But you wann to Interpret IT in another way.
    It is up to you to Do the rigth calculations...

    Use parsedatetime and use only one date but the two Different Times to calculations the two values
  3. Mahares

    Mahares Well Known Member Licensed User

    Dim OrigDateFormat As String = DateTime.dateFormat
    DateTime.dateFormat = "MM/dd/yyyy HH:mm"
    Dim p As Period
    p = DateUtils.PeriodBetween(
    DateTime.DateParse("07/29/2015 10:00"),DateTime.DateParse("07/30/2015 11:00"))
    Log(p.Days & " day(s) " & p.Hours & " hr(s)")  'displays 1 day 1 hr
    DateTime.dateFormat =OrigDateFormat
    DonManfred likes this.
  4. Roberto P.

    Roberto P. Well-Known Member Licensed User

    Thank you for the answers, but probably will not make myself clear.

    I need to calculate the number of hours that there are two periods of dates (with days apart) and I thought that the function PeriodBetween was able to do this calculation ElapsedTime

    Thank You
  5. DonManfred

    DonManfred Expert Licensed User

    even after this post i for myself i´m not sure to understand what exactly you want to do

    @Mahares showed you the right way to calculate the period between two dates.

    Maybe you should describe it more clearly what exactly is the base, what exactly do you want to archieve and what is the code you are trying to get this result but did not work
  6. Roberto P.

    Roberto P. Well-Known Member Licensed User

    OK my friend....

    this is a simple problem:

    start time and date = 07.01.2015 10:00
    end date and time = 07/25/2015 06:00

    how many hours?

    I thought that had already PeriodBetween function that returns the "sum of hours." While, returns the number of days and number of hours "part. In this case the hours are calculated -4.

    I repeat, I thought there was the function elapsedtime.
  7. Erel

    Erel Administrator Staff Member Licensed User

    If you are only interested in hours then it is simpler to calculate it directly from the ticks values:
    Sub HoursBetween (StartDate As Long, EndDate As Long) As Int
    Return Floor((EndDate - StartDate) / DateTime.TicksPerHours)
    End Sub
  8. Roberto P.

    Roberto P. Well-Known Member Licensed User

    great Erel
    This is exactly what I need. He must instantly code.
    Thank You
  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