We help IT Professionals succeed at work.

ASP Date format

mehdi
mehdi asked
on
People, a real quicky..

I have a date field in a database.  When i retrieve it, the format of the year is yy, rather than yyyy.  In the database it is yyyy !!

Why is it chopping off my yyyy ? How can I tell VBSCript that i want the date, the WHOLE date.. and nothing but the date ?

I do not want any changes to the database for this, maybe the format function ?

Mehdi
Comment
Watch Question

Commented:
Check your regional settings on the Server.
Change it from "yy" to "YYYY".
That should fix your problem.
 
VBScript Objects available for date manipulation

Date Returns the current date

DateSerial Converts month, day, and year values to a Date value

DateValue Returns a date part of a Date value

FormatDateTime Formats a value as a date or time
VBScript Objects available for date manipulation

Date Returns the current date

DateSerial Converts month, day, and year values to a Date value

DateValue Returns a date part of a Date value

FormatDateTime Formats a value as a date or time
CERTIFIED EXPERT
Top Expert 2005
Commented:
Something like this might work:


sub NiceDate(dDate)

dim strDay, strMonth, strYear
strDay = Day(dDate())
strMonth = Month(dDate())
strYear = Year(dDate())
if Int(strDay) <=9 then
     strDay = "0" & strDay
end if
if Int(strMonth) <=9 then
     strMonth = "0" & strMonth
end if

MsgBox strMonth & "/" & strDay & "/" &strYear


end sub

Commented:
One other VBscript object that may prove helpful is:

ISDate - which returns a "true" or "false" if the object in question can be interpreted as a date.

Author

Commented:
thanks for all your help with this visual BASIC problem :)

umm.. let me clear a few things up.. this *is* a date object.  It is comming from a database field of type date.

in the database the date is

06/07/2002

i do response.write rs("field") .. and it comes out as

06/07/02

hmm.. ok.  So, i assume that the date in its original format is still accessible its a case of telling VB to format it (with the format function maybe.. whih I find illusive).

As for the reagional settings, i want this code to work on the next server that it goes to aswell, so my only real option is to fix the problem in the code.

Mr Fritz.. nice suggestion.. i will try that out.

Thanks all

~~~~~~~~~~~~~~~~~~~~~~~~~~~
The secret lemonade drinker
CERTIFIED EXPERT
Top Expert 2005

Commented:
There may be no date format function, so that's why I wrote my own. Maybe this will work?

function NiceDate(dDate)

dim strDay, strMonth, strYear
strDay = Day(dDate())
strMonth = Month(dDate())
strYear = Year(dDate())
if Int(strDay) <=9 then
    strDay = "0" & strDay
end if
if Int(strMonth) <=9 then
    strMonth = "0" & strMonth
end if

return strMonth & "/" & strDay & "/" &strYear
end function

response.write( NiceDate(rs("field")))
CERTIFIED EXPERT

Commented:
We use LCID to set the locale of the session in asp. That makes the date format the same regardless of the setting of the server.

(The swedish date format (yyyy-mm-dd) has the advantage of being a standardized format, so it's accepted by sql. We seldom have problem with date format and databases.)

Sessions.LCID=1053 'swedish
Response.Write Date()

will produce something like this:

2001-10-22

Here's a locale id table:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsmscLCID.asp

Play with these, and I think that you'll find one that gives the output you want.

Commented:
hi  mehdi,
 Use Formatdatetime(yourdate,2). From Here you can select General Date,LongDate,LongTime,ShortDate,ShortTime as 1,2,3,4,5 Respectively.
 
 But kanthonym  is not explained properly about Formatdatetime funciton.

 Use this

Regards
 Pack

Author

Commented:

Explore More ContentExplore courses, solutions, and other research materials related to this topic.