Android Question DatePeriod to string

Discussion in 'Android Questions' started by trueboss323, Apr 2, 2015.

  1. trueboss323

    trueboss323 Active Member Licensed User

    Hi all,

    I am using DateUtlis to try to get the time period between 2 dates. I get the time period already in the ticks format and then I try to convert it into a time string so it displays something like 1 days 12 hours 29 minutes 51 seconds. Here is a code that I am currently using.
    Code:
    timeperiodlbl.text = (ConvertTicksToTimeString(ticks))

    Sub ConvertTicksToTimeString(t As Long) As String
        
    Dim  days, hours, minutes, seconds As Int
        days = t / 
    DateTime.TicksPerDay
        hours = t / 
    DateTime.TicksPerHour
        minutes = (t 
    Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
        seconds = (t 
    Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
        
    Return NumberFormat(days, 20) & "." _
            & 
    NumberFormat(hours, 20) & ":" & NumberFormat(minutes, 20) & ":" & NumberFormat(seconds, 20) & ""
    End Sub
    So the label text should appear as "1.12:29:51"
    but when I get a large time period, something greater than 9 days for example, the hours value go past 24. Here is a example 9.214:18:4
    So i suspect it is something to do with the Hours line inside the Sub but I cant figure out how to fix it. Can anyone help please?
     
  2. DonManfred

    DonManfred Expert Licensed User

    Code:
    Dim p As Period = Dateutils.PeriodBetween(startdate,enddate)
        
    Log(p)
     
  3. trueboss323

    trueboss323 Active Member Licensed User

    Thanks but how can i fix that from within the sub?
     
  4. Mahares

    Mahares Well Known Member Licensed User

    Here is an example where I use my date format. You can change your date format as fit: Displays:
    09.04:22:02

    Code:
    Log(ConvertTicksToTimeString("3/24/2015 11:23:15 AM","4/2/2015 3:45:17 PM"))
    Code:
    Sub ConvertTicksToTimeString(startdate As String, enddate As StringAs String
    DateTime.DateFormat="MM/dd/yyyy hh:mm:ss a"  'you can change your date format here
        Dim p As Period = Dateutils.PeriodBetween(DateTime.Dateparse(startdate),DateTime.Dateparse(enddate))
        
    Return NumberFormat(p.days, 20) & "." _
        & 
    NumberFormat(p.hours, 20) & ":" & NumberFormat(p.minutes, 20) & ":" & NumberFormat(p.seconds, 20)
    End Sub
    '
     
  5. trueboss323

    trueboss323 Active Member Licensed User

    I did it! I seemed to have fixed the problem now. all I did was change the sub to

    Code:
    Sub ConvertTicksToTimeString(t As Long) As String
        
    Dim  days, hours, minutes, seconds As Int
        days = t / 
    DateTime.TicksPerDay
        hours = (t 
    Mod DateTime.TicksPerDay) / DateTime.TicksPerHour
        minutes = (t 
    Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
        seconds = (t 
    Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
        
    Return NumberFormat(days, 20) & "." _
            & 
    NumberFormat(hours, 20) & ":" & NumberFormat(minutes, 20) & ":" & NumberFormat(seconds, 20) & ""
    End Sub
    Now it seems to be working fine.
     
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