strtime 12 hour clock

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,066
I am having trouble with a wintimer script.

I want to change the format to a 12 hour clock, can anyone help with this.

The format I am current using is.

Code:
Dim strTime As String
    strTime = Format$(Now(), "hh:mm:ss")
    'strTime = Format$(Now(), "h:mm:ss am/pm")

I have tried changing the hh to h etc... but nothing happens.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try:
Code:
Dim strTime As String
strTime = Format$(Now(), "[$-409]h:mm:ss AM/PM;@")
 
Upvote 0
Both scripts work well except they both have the am or pm on the end of the time, If I remove the am/pm from the script it reverts back to i.e. 13.00,

I would like the 13:00:01 to be 1:00:01 (no am or pm)
 
Upvote 0
Code:
Sub test()
Dim strTime As String, dblTime As Double
dblTime = Time
If dblTime > 0.5 Then dblTime = dblTime - 0.5
strTime = Format(dblTime, "h:mm:ss")
MsgBox strTime
End Sub

Note that this will convert all times to AM times, which is the only way to do what you want, and still have it be a time.
 
Upvote 0
Consider doing what you want this way...
Code:
Dim strTime As String
strTime = Split(Format$(Now, "h:mm:ss am/pm"))(0)
 
Upvote 0
I have gone with Rick's solution, thanks Rick,

With your script is there a way to isolate just the minutes and seconds, I tried removing hh but the time was not right.

Code:
Dim strTime As String
strTime = Split(Format$(Now, "h:mm:ss am/pm"))(0)
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top