Click to See Complete Forum and Search --> : Date format


Sid3335
04-21-2006, 11:54 AM
i have a database that logs visitors to my companies websites.

problem is I need to input the date into the database in a particular format:

yyyy-mm-dd which i believe is the mysql default.

how can i convert the date to this in asp:

the date is retrieved by the date() function but how can i format it from there.

btw, the date() function returns dd/mm/yyyy (uk format) and i can't change it using session.lcid because the code is plugged into many websites.

thank guys

lmf232s
04-21-2006, 01:28 PM
Something like this sould work. You could also pass in your delimiter (-, /) so you could make it useful for other date types as well.

The basic idea is to break the date apart.
Get the year, month, date and then put them back together they way you want them.

The could be written many of ways but here is the basic idea. Do with it as you please to make it work for you.

FormatMediumDate(Date())

Function FormatMediumDate(DateValue)
Dim strYYYY : strYYYY = cstr(DatePart("yyyy", DateValue))
Dim strMM : strMM = cstr(DatePart("m", DateValue))
Dim strDD : strDD = cstr(DatePart("d", DateValue))

If Len(strMM) = 1 then strMM = "0" & strMM
If Len(strDD) = 1 then strDD = "0" & strDD

FormatMediumDate = strYYYY & "-" & strMM & "-" & strDD
end function