Android Question [SOLVED] Return a leading zero if needed using DateTime.GetHour

Discussion in 'Android Questions' started by rleiman, Apr 23, 2018.

  1. rleiman

    rleiman Well-Known Member Licensed User

    Hi Everyone,

    Using the code below, is there a way to have GetHour and GetMinute display a "0" in front of the string if the returned value is less than 10? If the converted hours = "9" for 9 in the morning, I would like the returned string to have "09" in it.

    Code:
    Dim strMyTimeString As String = _
            
    DateTime.GetHour(lngMyTics) & ":" & DateTime.GetMinute(lngMyTics)
    Thanks
     
  2. udg

    udg Expert Licensed User

    "DateTime.GetHour" returns an INT; you could use NumberFormat to add a leading zero.
    Something like:
    NumberFormat(DateTime.GetHour(lngMyTics), 2 ,0)
    Or, even better, smart string literals:
    Code:
    Dim strMyTimeString As String =  _
    $"$2{DateTime.GetHour(lngMyTics)}:$2{DateTime.GetMinute(lngMyTics)}"$
     
    Last edited: Apr 23, 2018
    Erel, Peter Simpson and rleiman like this.
  3. rleiman

    rleiman Well-Known Member Licensed User

    Hi udg,

    Thanks for the quick reply. Wow. NumberFormat is sure handy!
     
  4. Peter Simpson

    Peter Simpson Expert Licensed User

    Or you could have just used at the beginning of your code.

    Code:
    DateTime.TimeFormat = "hh:mm"
    Or am I missing something here.

    Enjoy...
     
    rleiman likes this.
  5. rleiman

    rleiman Well-Known Member Licensed User

    Hi Peter,

    I will try that one as well. Looks like that one has more flexibility.

    Cheers.
     
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