|
-
Jan 30th, 2002, 12:01 PM
#1
Thread Starter
Fanatic Member
Formatting a date with vbscript in ASP
I'm trying to format a date before I display it on a web page
I know how to do it in Visual Basic eg =
Text2.Text = Format(Text1.Text, "dd/mm/yyyy")
I was hoping to use something similar in ASP ServerSide script
eg =
<%=format(rsWP(16),"dd/mm/yyyy")%>
But It doesn't work.
When I save the date to my DB it is sent and stored in the db as dd/mm/yyyy but when I display it on a web page it comes out as m/d/yyyy very annoying!
Help please?
Regards Al
-
Jan 30th, 2002, 02:05 PM
#2
First of all; if your just wanting to swap the month and day you may want to change your local. The local controls how dates and currency are formated. Typicly, it is set for 1033; which is 'English United States'. I have had to develop an international site before were the dates were formated differently for different users based on their region of the world.
Check out the following code.
Code:
<% Session.LCID = 1033 %>
<br>English-United States: <% FormatDateTime(Date(), vbLongDate) %>
<% Session.LCID = 3084 %>
<br>French-Canada: <% FormatDateTime(Date(), vbLongDate) %>
I used vbLongDate for the example because I wanted to show that it translates the name of the month; but you probably want to use vbShortDate.
Also, I guess you have already figured out that the format function isn't available in VBScript. I would check out the VBScript language reference if you are coming from VB or VBA.
Here is a function that you could use if your just wanting the padded zeroes.
Code:
<%
rsWP = cDate("1/30/2002")
response.write cDateFormat(rsWP) & " <br><br>"
function cDateFormat(myDate)
mYear = Year(myDate)
mMonth = Month(myDate)
if len(cstr(mMonth)) = 1 then
mMonth = "0" & mMonth
end if
mDay = Day(myDate)
if len(cstr(mDay)) = 1 then
mDay = "0" & mDay
end if
cDateFormat = mMonth & "/" & mDay & "/" & mYear
end function
%>
-
Jan 30th, 2002, 02:09 PM
#3
The local ID for 'English-United Kingdom' is 2057.
-
Jan 31st, 2002, 04:08 AM
#4
Thread Starter
Fanatic Member
Red Rush In
Thanks for your help. I've checked out the web site as advised should be a great help in the future.
I've not been able to get the session.LCID to work I get two error messages back saying 'object required' and 'Can't use parenthesis when calling a sub' Don't know?
I've used your function
CdateFormat which does what I need it to do great!
I have one concern, but will test it properly before I implement it, and that is how it interprets dates like 1/6/2002 how will it know whether it's the 1st of June or the 6th of January? I guess it's a draw back of using the short date.
-
Jan 31st, 2002, 11:28 AM
#5
Oops! I forgot the '=' sighns for the function calls.
Code:
<% Session.LCID = 1033 %>
<br>English-United States: <% =FormatDateTime(Date(), vbLongDate) %>
<% Session.LCID = 3084 %>
<br>French-Canada: <% =FormatDateTime(Date(), vbLongDate) %>
That should work better; I tested it this time.
-
Jan 31st, 2002, 11:58 AM
#6
Thread Starter
Fanatic Member
Rush-in,
I can't work out what the problem is?? I'm still getting an error message 'Object Required' on the line with
<% Session.LCID = 1033 %>
Do I need to set the meta data / char set?? As you can tell I quite new to web dev. I'm sure it's something simple.
Cheers
-
Jan 31st, 2002, 04:35 PM
#7
Hmm, thats weird.
Now, I assumed because you said you were using a database that you are doing this in ASP. Are you using IIS and if so what version.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|