Time Format

T

Travis Pupkin

Hi,

When pulling the hour (or minute, or second, or day, or month, etc) from
the server like:

<%= hour(time) %>

Is there a quick way (like, without an if then statement to check it and
fix it) to format it so it represents all hours in two digits like:

00

for 12 am. instead of just:

0

?

Thanks.
 
C

Chris Barber

It's a pity that FormatDateTime won't do what you want, nor will
FormatCurrency etc.

You're probably stuck with creating a small function to return the formatted
string eg.

Function AddExtraZerosIfReq(plngValue)
Dim pstrValue
Dim plngChars
Dim i
pstrValue = CStr(plngValue)
plngChars = Len(pstrValue)
If plngChars < 2 Then
For i = 1 to plngChars Step -1
pstrValue = "0" & pstrValue
Next
End If
AddExtraZerosIfReq = pstrValue
End Function

And referencing it as:

<%= AddExtraZerosIfReq(Hour(time)) %>

NB: This would be easy in Visual Basic, it would be:

Format(time, "00")

Why they missed this obvious functionality from VBScript is beyond me!

Hope this helps,

Chris.

Hi.

Take a look at function FormatDateTime():
http://msdn.microsoft.com/library/d...y/en-us/script56/html/vsfctformatdatetime.asp
 
C

Chris Barber

<in awe>
I didn't see that particular solution but I like it.
</in awe>

Chris.

Or more simply <%=Right('0' & Hour(Time()),2)%>

HTH

Chris
 
T

Travis Pupkin

chris@blue- said:
<in awe>
I didn't see that particular solution but I like it.
</in awe>

Chris.


Don't be too awed Chris, your suggested worked much better. Although I
would prefer something as simple CJM's code, it didn't account for the
occasional two digits (and would replace the first of the two with a
'0', so at 2 p.m., instead of '14' you'd get '04'.

But your function is as close as I can get to something lean.

Thanks. This group always kicks ass.
 
C

Chris Barber

Errm,

Wouldn't it just do:

Hour = "14"

Append a zero to give "014" and then take the rightmost two digits to give
"14" again.

Chris.

chris@blue- said:
<in awe>
I didn't see that particular solution but I like it.
</in awe>

Chris.


Don't be too awed Chris, your suggested worked much better. Although I
would prefer something as simple CJM's code, it didn't account for the
occasional two digits (and would replace the first of the two with a
'0', so at 2 p.m., instead of '14' you'd get '04'.

But your function is as close as I can get to something lean.

Thanks. This group always kicks ass.
 
C

Chris Barber

A possible typo for VBScript [strings]?

<%=Right('0' & Hour(Time()),2)%>

should be:

<%=Right("0" & Hour(Time()),2)%>

Double quotes instead of single-quotes?

Chris.


chris@blue- said:
<in awe>
I didn't see that particular solution but I like it.
</in awe>

Chris.


Don't be too awed Chris, your suggested worked much better. Although I
would prefer something as simple CJM's code, it didn't account for the
occasional two digits (and would replace the first of the two with a
'0', so at 2 p.m., instead of '14' you'd get '04'.

But your function is as close as I can get to something lean.

Thanks. This group always kicks ass.
 
A

Aaron Bertrand [MVP]

Don't be too awed Chris, your suggested worked much better. Although I
would prefer something as simple CJM's code, it didn't account for the
occasional two digits (and would replace the first of the two with a
'0', so at 2 p.m., instead of '14' you'd get '04'.

Did you actually try it, or are you just grasping at straws here?

Notice this:

right("00000000000000000000" & yournumber, 2)

will still only generate a 2-digit number.
 
C

Chris Barber

I'd use CJM's code instead of my own - it'll be a lot quicker especially for
larger numbers of outputs (eg a report) and should easily lend itself to
using a comparable technique in JavaScript where rounding decimal places and
retaining / adding trailing zeros is a complete arse.

Chris.

Aaron Bertrand said:
Don't be too awed Chris, your suggested worked much better. Although I
would prefer something as simple CJM's code, it didn't account for the
occasional two digits (and would replace the first of the two with a
'0', so at 2 p.m., instead of '14' you'd get '04'.

Did you actually try it, or are you just grasping at straws here?

Notice this:

right("00000000000000000000" & yournumber, 2)

will still only generate a 2-digit number.
 
C

CJM

Hero to Zero, eh? lol

Ok, it should have been double quotes...

My excuse is I was working in Javascript at the time...

Cheers

Chris
 
T

Travis Pupkin

chris@blue- said:
I'd use CJM's code instead of my own - it'll be a lot quicker especially for
larger numbers of outputs (eg a report) and should easily lend itself to
using a comparable technique in JavaScript where rounding decimal places and
retaining / adding trailing zeros is a complete arse.

Chris.



Did you actually try it, or are you just grasping at straws here?

Yeah, I tried it. It was 2 p.m. and I got 04. So I went with Chris'
function.
 
A

Aaron Bertrand - MVP

Yeah, I tried it. It was 2 p.m. and I got 04.

Can you show the code that did this? I just copied the exact same code and
got 13 (it's 1:35 PM here).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top