Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a time() value to a string 1

Status
Not open for further replies.

Jackie

MIS
Feb 9, 2000
148
0
0
US

How do you convert a time() value to a string and not include the colon.

For example, if time() = 13:05:22. I would like to capture it as 1305.

I tried the str(time()), but got an error. I also tried using str(time() format"HHMM") but got an error too.

I am collecting the current time on a form using =time().
I would like to convert a time field to hhmm format using 24 hour GMT (without the colon).

Thank you for your help.
 
Here is a Function that you can COPY and Paste into a database module:
Public Function ReplaceEmbedded(CharToReplace As String, SearchString As String, ReplacementChar As String)
Dim I As Integer, s As String, ss As Integer
ss = Len(SearchString)
s = ""
For I = 1 To ss
If InStr(CharToReplace, Mid$(SearchString, I, 1)) = 0 Then
s = s & Mid$(SearchString, I, 1)
Else
s = s & ReplacementChar
I = I + (Len(CharToReplace) - 1)
End If
Next I
ReplaceEmbedded = s
End Function
Use the following expression to capture the string value you indicated you needed in your post:
Mid$(ReplaceEmbedded(":", CStr(Time()), ""), 1, Len(CStr(Time())) - 7)

Bob Scriver
 
You nearly had the answer yourself, but try this:
=Str(Format(Time(),"hhnn"))

Not sure why you'd want this field to be a string value so note that the leading zero will be ommitted as I presume it looks at Time() as and integer before it converts to string, which would of course take for example 0700 as 700.

However, a simple 'IIf' test could easily add the leading zero back on. (unless someone knows of a way to keep it on):
=IIf(Len(Trim(Str(Format(Time(),"hhnn"))))=3,"0" & Trim(Str(Format(Time(),"hhnn"))),Trim(Str(Format(Time(),"hhnn"))))

Good Luck
[yinyang]
 
Actually, leading zeros will be preserved:

X = "07:30AM"

? format(x, "hhnn")

0730

The Format guy automatically makes a string, so you don't need the STR function.

? format(x, "hhnn") & " is in the morning"

0730 is in the morning




Me? Ambivalent? Well, yes and no....
Another free Access forum:
More Access stuff at
 
Thanks Wildhare

Yeah - umm I wasn't exactly sure if the str() function was entirely necessary...but could you explain something for me, why does it cut the leading zero off when the str() function IS added?

Example:
[ul][li]format(x, "hhnn") = 0730[/li][li]Str(format(x, "hhnn")) = 730[/li][/ul]
...why is this? Does function str() do something more than convert a value to string because I would have presumed it would have preserved "0730" when doing a str() conversion. Or does format() and/or str() automatically make the 'best' decision as to what value type it should be?
[yinyang]

 
I've found that very few Access functions make the best decisions... [hammer]

I really don't know why STR performs this digital briss to the forefront of a numeric guy, but FORMAT does it too:

? format("08976") returns "8976"

It seems that unless you provide a SPECIFIC, leading-zero preserving format like

? Format("008976", "000000")

it just drops the leading naughts, just like STR() does.

Note that, given a TIME of 07:30AM,

Format(x, "hn") returns 730
but
Format(x, "hhnn") returns 0730
as does
format(x, "hhn")

Just gotta get used to it, I suppose.

format(chr(74) & chr(77) & chr(72))





Me? Ambivalent? Well, yes and no....
Another free Access forum:
More Access stuff at
 
More effecient and very helpful. Good explainations, too. Have a star from me.!!!
[2thumbsup]

Bob Scriver
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top