1

How would I be able to extract the time in hh:mm am/pm from a date in vbscript?

For example, with the date '06/25/19 02:10:12 PM', I would only like to show '2:10 PM'

date = CDate("06/25/19 02:10:12 PM")
FormatDateTime(date, vbLongTime)

This will return 2:10:12 PM but seconds should not be shown.

date = CDate("06/25/19 02:10:12 PM")
FormatDateTime(date, vbShortTime)

This will return 14:10 but this is in military time.

I've seen functions in C# and VB.NET that will directly return the time in hh:mm am/pm but was unable to find documentation on functions that will do so in VBScript.

azyssd
  • 21
  • 1
  • 6
  • Try `vbLongTime`. – user692942 Jun 25 '19 at 20:10
  • Possible duplicate of [How do I get the Date & Time (VBS)](https://stackoverflow.com/questions/7011357/how-do-i-get-the-date-time-vbs) – user692942 Jun 25 '19 at 20:12
  • vbLongTime returns a time that includes seconds which is what I'm trying to remove. The answer in the other link provided uses the Format function in VB.NET and not VBScript – azyssd Jun 25 '19 at 20:42
  • [If you are getting the system time I have this very silly VBScript here](https://stackoverflow.com/a/53774325/129130). And it is not the same issue, but I encountered the [WbemScripting.SWbemDateTime](https://stackoverflow.com/q/46762018/129130) object. Try to skim that answer? – Stein Åsmul Jun 25 '19 at 20:57
  • Just throwing in a link to the [vbsedit.com samples repository](http://www.vbsedit.com/scripts/default.asp) - an excellent source of information on VBScript. Search for **`"WbemScripting.SWbemDateTime"`** via [the search page](http://www.vbsedit.com/scripts/search.asp). – Stein Åsmul Jun 25 '19 at 21:03
  • Just reading the accepted answer is not usually the right thing to do, its clear from the comments that answer is to do with VB (not VB.Net). Did you even look at [the upvoted answer](https://stackoverflow.com/a/19939013/692942)? You don't want seconds, fine build the format yourself using the `Hour()` and `Minute()` functions, you can even `Mod 12` the `Hour()` function to workout the hour in 12-hour time. – user692942 Jun 25 '19 at 22:00
  • 1
    Sorry you're right, I should've taken more time to look through that forum. I looked through the first couple of answers and didn't see what I saw looking for so I moved on. The upvoted answer helped me get the code working. Thanks – azyssd Jun 25 '19 at 22:44

1 Answers1

0

You can do this by building your own custom formatting function using the built-in time functions Hour() and Minute().

Function FormatTime(dateIn, asTwelveHourFormat)
  Dim result, h, m, t
  If IsDate(dateIn) Then
    If Len(asTwelveHourFormat & "") < 1 Then asTwelveHourFormat = False
    h = Hour(dateIn)
    m = Minute(dateIn)
    t = ""
    If asTwelveHourFormat Then
      t = " "
      If h >= 12 Then
        t = t & "PM"
        If h > 12 Then h = h Mod 12 
      Else 
        t = t & "AM"
      End If
    End If
    h = Right("00" & h, 2)
    m = Right("00" & m, 2)
    result = h & ":" & m & t
  Else
    result = "NA"
  End If
  FormatTime = result
End Function

Code is provided un-tested, just wrote it now on my iPad so not in a position to test it.


Useful Links

user692942
  • 16,398
  • 7
  • 76
  • 175