Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change the date format from dd/mm/yyy to mm/dd/yyyy in ASP

Status
Not open for further replies.

mellynda

Programmer
Mar 14, 2003
2
FR
Hello,
I'm french and the default date format is "dd/mm/yyyy".
I would like to change this format into "mm/dd/yyyy" in order to use it into a sql request.
I've found the fonction in VBA : format(date,"mm/dd/yyyy")
but it doesn't work in ASP.
Does someone has the answer?
Thanks
 
This is my facorite function. There are simpler ways to do a date month(date) "/" & etc... but this is a great overall function.

Enjoy,
Aaron

For your use:
<%
dMyDate=MyFormatDate(dMyDate,&quot;%d/%m/%Y&quot;)

Function MyFormatDate (strDate, strFormat)

'********Formats*********
'%m Month as a decimal no. 02
'%b Abbreviated month name Feb
'%B Full month name February
'%d Day of the month 23
'%j Day of the year 54
'%y Year without century 98
'%Y Year with century 1998
'%w Weekday as integer 5 (0 is Sunday)
'%a Abbreviated day name Fri
'%A Weekday Name Friday
'%I Hour in 12 hour format 12
'%H Hour in 24 hour format 24
'%M Minute as an integer 01
'%S Second as an integer 55
'%P AM/PM Indicator PM
'%% Actual Percent sign %%



Dim intPosItem
Dim intHourPart
Dim strHourPart
Dim strMinutePart
Dim strSecondPart
Dim strAMPM


If not IsDate(strDate) Then
DPFormatDate = strDate
Exit Function
End If

intPosItem = Instr(strFormat, &quot;%m&quot;)
Do While intPosItem > 0
If DatePart(&quot;m&quot;,strDate)<10 then LeadingZero = &quot;0&quot; End if
strFormat = Left(strFormat, intPosItem-1) &LeadingZero& _
DatePart(&quot;m&quot;,strDate) & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%m&quot;)
Loop
LeadingZero = &quot;&quot;

intPosItem = Instr(strFormat, &quot;%b&quot;)
Do While intPosItem > 0
strFormat = Left(strFormat, intPosItem-1) & _
MonthName(DatePart(&quot;m&quot;,strDate),True) & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%b&quot;)
Loop

intPosItem = Instr(strFormat, &quot;%B&quot;)
Do While intPosItem > 0
strFormat = Left(strFormat, intPosItem-1) & _
MonthName(DatePart(&quot;m&quot;,strDate),False) & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%B&quot;)
Loop

intPosItem = Instr(strFormat, &quot;%d&quot;)
Do While intPosItem > 0
If DatePart(&quot;d&quot;,strDate)<10 then LeadingZero = &quot;0&quot; End if
strFormat = Left(strFormat, intPosItem-1) &LeadingZero& _
DatePart(&quot;d&quot;,strDate) & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%d&quot;)
Loop
LeadingZero = &quot;&quot;

intPosItem = Instr(strFormat, &quot;%j&quot;)
Do While intPosItem > 0

strFormat = Left(strFormat, intPosItem-1) & _
DatePart(&quot;y&quot;,strDate) & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%j&quot;)
Loop

intPosItem = Instr(strFormat, &quot;%y&quot;)
Do While intPosItem > 0
strFormat = Left(strFormat, intPosItem-1) & _
Right(DatePart(&quot;yyyy&quot;,strDate),2) & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%y&quot;)
Loop

intPosItem = Instr(strFormat, &quot;%Y&quot;)
Do While intPosItem > 0
strFormat = Left(strFormat, intPosItem-1) & _
DatePart(&quot;yyyy&quot;,strDate) & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%Y&quot;)
Loop

intPosItem = Instr(strFormat, &quot;%w&quot;)
Do While intPosItem > 0
strFormat = Left(strFormat, intPosItem-1) & _
DatePart(&quot;w&quot;,strDate,1) & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%w&quot;)
Loop

intPosItem = Instr(strFormat, &quot;%a&quot;)
Do While intPosItem > 0
strFormat = Left(strFormat, intPosItem-1) & _
WeekDayName(DatePart(&quot;w&quot;,strDate,1),True) & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%a&quot;)
Loop

intPosItem = Instr(strFormat, &quot;%A&quot;)
Do While intPosItem > 0
strFormat = Left(strFormat, intPosItem-1) & _
WeekDayName(DatePart(&quot;w&quot;,strDate,1),False) & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%A&quot;)
Loop

intPosItem = Instr(strFormat, &quot;%I&quot;)
Do While intPosItem > 0
intHourPart = DatePart(&quot;h&quot;,strDate) mod 12
if intHourPart = 0 then intHourPart = 12
strFormat = Left(strFormat, intPosItem-1) & _
intHourPart & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%I&quot;)
Loop

intPosItem = Instr(strFormat, &quot;%H&quot;)
Do While intPosItem > 0
strHourPart = DatePart(&quot;h&quot;,strDate)
if strHourPart < 10 Then strHourPart = &quot;0&quot; & strHourPart
strFormat = Left(strFormat, intPosItem-1) &LeadingZero& _
strHourPart & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%H&quot;)
Loop


intPosItem = Instr(strFormat, &quot;%M&quot;)
Do While intPosItem > 0
strMinutePart = DatePart(&quot;n&quot;,strDate)
if strMinutePart < 10 then strMinutePart = &quot;0&quot; & strMinutePart
strFormat = Left(strFormat, intPosItem-1) & _
strMinutePart & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%M&quot;)
Loop

intPosItem = Instr(strFormat, &quot;%S&quot;)
Do While intPosItem > 0
strSecondPart = DatePart(&quot;s&quot;,strDate)
if strSecondPart < 10 then strSecondPart = &quot;0&quot; & strSecondPart
strFormat = Left(strFormat, intPosItem-1) & _
strSecondPart & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%S&quot;)
Loop

intPosItem = Instr(strFormat, &quot;%P&quot;)
Do While intPosItem > 0
if DatePart(&quot;h&quot;,strDate) >= 12 then
strAMPM = &quot;PM&quot;
Else
strAMPM = &quot;AM&quot;
End If
strFormat = Left(strFormat, intPosItem-1) & _
strAMPM & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%P&quot;)
Loop

intPosItem = Instr(strFormat, &quot;%%&quot;)
Do While intPosItem > 0
strFormat = Left(strFormat, intPosItem-1) & &quot;%&quot; & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, &quot;%%&quot;)
Loop

MyFormatDate = strFormat

End Function%>
 
I was going to pick on palbano but sense I have done that same thing so many times I won't [smile]

I think he meant this faq333-3194 _________________________________________________________
for the best results to your questions: FAQ333-2924
01001111 01101110 01110000 01101110 01110100
onpnt2.gif
[/sub]
 
I am in NZ and we have dd/mm/yyyy too.
I've just spent ages playing with dates... the best I ended up doing was creating a function ValidateDate as I use drop down boxes etc on a form...

but all you really need is
dday = day(YourDate)
dmonth = month(YourDate)
if Len(dday)=1 Then dday = &quot;0&quot; & dday
if Len(dmonth)=1 Then dmonth = &quot;0&quot; & dmonth
DatabaseDate = dmonth & &quot;/&quot; & dday & &quot;/&quot; & year(YourDate)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top