Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

Convert DateTime value to strings

Here is my current VBA conversion

lngGameBoardNumber = Format(Date, "yyyy") + Format(Date, "mm") + Format(Date, "dd") + Format(Time, "hh") + Format(Time, "nn") + Format(Time, "ss")

This means that tody's date + time would be converted to:

20030621103843

in 5 seconds it would look like

20030621103848


But I want to change this so it will work under ASP / VBScript:


Avatar of Gary
Gary
Flag of Ireland image

Use FormatDateTime(lngGameBoardNumber,vblongtime)
try this

<%
lngGameBoardNumber = Year(Date) & Month(Date) & Day(Date) & Hour(Time) & Minute(Time) & Second(Time)
response.write lngGameBoardNumber
%>


hongjun
Or do you mean

lngGameBoardNumber = DatePart(Date, "yyyy") & DatePart(Date, "m") & DatePart(Date, "d") & DatePart(Time, "h) & DatePart(Time, "n") & DatePart(Time, "s")
Avatar of Tom Knowlton

ASKER

I get:

sGameNum = DatePart(Date, "yyyy") & DatePart(Date, "m") & DatePart(Date, "d") & DatePart(Time, "h) & DatePart(Time, "n") & DatePart(Time, "s")     Set oGenID = Nothing
---------------------------------------------------------------------------------------------------------------------^
sGameNum = DatePart(Date, "yyyy") & DatePart(Date, "m") & DatePart(Date, "d") & DatePart(Time, "h") & DatePart(Time, "n") & DatePart(Time, "s")


hongjun
nevermind...caught the bug...

testing it now...
You may also wish to consider my first comment.
It should be this

sGameNum = DatePart("YYYY", Date) & DatePart("M", Date) & DatePart("D", Date) & DatePart("H", Time) & DatePart("N", Time) & DatePart("S", Time)


hongjun
The interval should be the first argument.

hongjun
Well, I thought I had it fixed.  Now I get:

Microsoft VBScript runtime error '800a000d'

Type mismatch: '[string: "yyyy"]'

/robotzgame/game.asp, line 75


Here is the current code:

 Dim sGameNum

    sGameNum = DatePart(Date, "yyyy") & DatePart(Date, "m") & DatePart(Date, "d") & DatePart(Time, "h") & DatePart(Time, "n") & DatePart(Time, "s")    

knowlton, see the code above.
Oooooops.

I'll make the change!

Tom
Use either
<%
sGameNum = Year(Date) & Month(Date) & Day(Date) & Hour(Time) & Minute(Time) & Second(Time)
response.write sGameNum
%>


OR
<%
sGameNum = DatePart("YYYY", Date) & DatePart("M", Date) & DatePart("D", Date) & DatePart("H", Time) & DatePart("N", Time) & DatePart("S", Time)
response.write sGameNum
%>

hongjun
Okay...it works....but still needs ONE tweak.

I need the Month, Day, Hour, Minutes, and Seconds to be formatted at ##   instead of  #

for example, June would be

06

instead of

6



Can DatePart handle this?
Try this

<%
sGameNum = DatePart("YYYY", Date) & Right("0" & DatePart("M", Date), 2) & Right("0" & DatePart("D", Date), 2) & Right("0" & DatePart("H", Time), 2) & Right("0" & DatePart("N", Time), 2) & Right("0" & DatePart("S", Time), 2)
response.write sGameNum
%>



hongjun
ASKER CERTIFIED SOLUTION
Avatar of hongjun
hongjun
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works great, thanks.

Tom
50 more points for your trouble.