Example - Hour Function

  1. ' This example shows various uses of the Format function to format values

    ' using both named and user-defined formats. For the date separator (/),

    ' time separator (:), and AM/ PM literal, the actual formatted output

    ' displayed by your system depends on the locale settings on which the code

    ' is running. When times and dates are displayed in the development

    ' environment, the short time and short date formats of the code locale

    ' are used. When displayed by running code, the short time and short date

    ' formats of the system locale are used, which may differ from the code

    ' locale. For this example, English/United States is assumed.

     

    ' MyTime and MyDate are displayed in the development environment using

    ' current system short time and short date settings.

     

    Sub Main ()

    1. MyTime = "08:04:23 PM"

      MyDate = "03/03/95"

      MyDate = "January 27, 1993"

      MsgBox Now 'Returns the current date and time

      MsgBox MyTime 'Returns "08:04:23 PM"

      MsgBox Second( MyTime ) & " Seconds" 'Returns "23 Seconds"

      MsgBox Minute( MyTime ) & " Minutes" 'Returns "4 Minutes"

      MsgBox Hour( MyTime ) & " Hours" 'Returns "20 Hours"

      MsgBox Day( MyDate ) & " Days" 'Returns "27 Days"

      MsgBox Month( MyDate ) & " Months" 'Returns "1 Months"

      MsgBox Year( MyDate ) & " Years" 'Returns "1993 Years"

       

      MsgBox Format(Time, "Short Time")

      'Returns the current system time in this short format "hh:mm"

       

      MsgBox Format(Time, "Long Time")

      'Returns the current system time in this long format "hh:mm:ss pm"

       

      MsgBox Format(Date, "Short Date")

      'Returns the current system date in this short format "mm/dd/yyyy"

       

      MsgBox Format(Date, "Long Date")

      'Returns the current system, date in the long format.

       

      'Returns the time or date based on the parameters:

      MsgBox Format(MyTime, "h:n:s") ' Returns "17:4:23".

      MsgBox Format(MyTime, "hh:nn:ss AMPM") ' Returns "05:04:23 PM".

      MsgBox Format(MyDate, "dddd, nnn d yyyy") ' Returns "Wednesday, Jan 27 1993".

       

      ' If format is not supplied, a string is returned:

      MsgBox Format(23) ' Returns "23".

       

      ' User-defined formats:

      MsgBox Format(5459.4, "##,##0.00") ' Returns "5,459.40".

      MsgBox Format(334.9, "###0.00") ' Returns "334.90".

      MsgBox Format(5, "0.00%") ' Returns "500.00%".

      MsgBox Format("HELLO", "<") ' Returns "hello".

      MsgBox Format("This is it", ">") ' Returns "THIS IS IT".

     

    End Sub