strtime 12 hour clock

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,060
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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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,216,075
Messages
6,128,660
Members
449,462
Latest member
Chislobog

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