1. Jim D. Guest

Does anyone have a sub or function that will remove the :00 (seconds) from a time? E.g., I have a time generated from a loop that looks like this: 7:00:00 PM. What I want is 7:00 PM. Could someone direct me in the proper way to do this?

2. Steve Cimino Guest

## RE: Remove the seconds, please

A few options:<BR><BR>str = Hour(time) & ":" & Minute(time)<BR><BR>LEFT - but be careful of the differences between 7:00 and 12:00 (digit placement)<BR><BR>SPLIT - however, seems like a waste

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Remove the seconds, please

There are several ways to do this.<BR><BR>(1) Generate the time string "by hand". Thus:<BR><BR>&#060;%<BR>theTime = ... **NOT a string!** ...<BR>theHour = Hour( theTime )<BR>If theHour &#062; 12 Then <BR> &nbsp; theHour = theHour - 12<BR> &nbsp; ampm = " PM"<BR>Else<BR> &nbsp; ampm = " AM"<BR> &nbsp; If theHour = 0 Then theHour = 12<BR>End If<BR><BR>strTime = theHour & ":" & Right( "0" & Minute(theTime), 2 ) & ampm<BR>%&#062;<BR><BR>Complicated, isn&#039t it?<BR><BR>********************<BR><BR>(2) Manipulate the given string.<BR><BR>I assume here that you have the time in a string of the form "9:31:22 PM" or some such.<BR><BR>&#060;%<BR>strTime = ... string form of time ...<BR>colon = InStrRev( strTime, ":" ) &#039 find last : in the string<BR>strTime = Left( strTime, colon-1 ) & Mid( strTime, colon+3 ) &#039 thus cut out seconds<BR>%&#062;<BR><BR>**************<BR><BR>Pr obably other ways. Looks like number 2 is reasonably efficient, though.<BR><BR>

4. Steve Cimino Guest

## For a 24hour clock

Hour returns a military 24 hour clock. Forgot to mention that. You may want to try something different.

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Not even 24 hour time...

&nbsp;<BR>&#062; str = Hour(time) & ":" & Minute(time)<BR><BR>Sorry, Steve. But that will produce times such as 7:3 and 9:2<BR>for minutes less than 10.<BR><BR>You need something like<BR><BR>str = Hour(time) & ":" & Right( "0" & Minute(time), 2 )<BR><BR>to tack a leading 0 on the minutes if they are only a single digit.<BR><BR>[ gotcha! ]<BR>[ grin ]<BR><BR>

6. Jim D. Guest

## RE: Remove the seconds, please

Actually, I have the time in a date format. I guess I need to typecast it to a str?

7. Steve Cimino Guest

## Yep, you're right, but this is easier

I forgot about that...<BR><BR>Don&#039t you hate when you begin a post and get caught up doing something else (as in my lunch arriving) and you want to get the answer out quickly?<BR><BR>And, it won&#039t print out AM/PM, like you have in your other post.<BR><BR>However, wouldn&#039t the format be MUCH easier?<BR><BR>myTime = FormatDateTime("7:00:00", vbShortTime)<BR><BR>Possibly no use of quotes... won&#039t take the time to research it since I&#039m eating. See how much quicker I can think when I;m not doing something else?

8. Steve Cimino Guest

## **** it, that's also 24 hour time

I&#039m not posting anymore while eating.<BR><BR>Can&#039t walk and chew gum at the same time.

9. Jim D. Guest

## RE: **** it, that's also 24 hour time

Yep... Tried that a while back.<BR><BR>Enjoy your food. Wish I was eating.

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Remove the seconds, please

&#062; Actually, I have the time in a date format. I guess I need to typecast it to a str?<BR><BR>Sure...just do <BR> &nbps; strTime = CStr( TimeValue( yourDateVariable ) )<BR>and proceed as in version 2.<BR><BR>TimeValue extracts *just* the time from a Date/Time value, so you may not need it, of course.<BR><BR>(If you need both date and time, though, version 2 works fine. It won&#039t touch any date that preceeds the time.)<BR><BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•