Hi,

The date is being formatted according to the user's regional settings (in
the control panel).  If your users are in many countries, then this is the
correct behaviour;  Dates entered by Australians would be automatically
interpreted as dd/mm/yyyy whilst dates entered by Americans would be
interpreted as mm/dd/yyyy.

This works very well if the user's control panel settings are correct.  If
your users are all in one country, then you can make some assumptions about
the user's intentions, otherwise you cannot.

It is for this reason that most websites use drop-downs for they day and
month - it avoids any ambiguity.

If your users *are* all in one country (eg for an intranet application),
then you can extract the day and month using "Mid", and recombine them using
"DateSerial".

Of course, if the string to date conversion occurs on the server, then date
will be interpreted according to the regional settings on the server, as in
the following example "thispage.asp":



<% @language='VbScript' %>

<%

if Request.QueryString("MyDate").Count>0 then

  sDate=Request.QueryString("MyDate")

  sFormattedDate=FormatDateTime(sDate, vbShortDate)

else

  sDate=""

  sFormattedDate=""

end if

%>

<html>

        <head>

                <meta http-equiv="content-type"
content="text/html;charset=iso-8859-1">

                <title>Test Page</title>

        </head>

        <body bgcolor="#ffffff">

                <%

                        Response.write "<p>" & sDate & "</p>"

'                       Response.write "<p>" & sFormattedDate & "</p>"

                %>

                <form method="GET" action="thispage.asp" name="MyForm">

                        <input type="text" name="MyDate" size="24">

                        <p><input type="submit"></p>

                </form>

        </body>



</html>





...But I digress.

To answer your original question, which seemed to be about client-side code,
I present the following modified version of your code:

 

<html>



<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<title>New Page 1</title>

<meta name="GENERATOR" content="Microsoft FrontPage 6.0">

<meta name="ProgId" content="FrontPage.Editor.Document">

<script Language="VBScript">

'This example is culture-specific, and should not be used on

'sites that are intended to be used in countries whose date

'format is other than dd/mm/yyyy.

Function IsDigit(s)

 '2004/10/12 ALH

 'This function returns True, if the first character in s

 'is a digit.

 'ie, a character in the range "0".."9"

 if asc(s)>=asc("0") and asc(s)<=asc("9") then

   IsDigit = True

 else

   IsDigit = False

 end if

End Function



Function ContainsOnlyDigits(s)

 '2004/10/12 ALH

 'This function returns True, if s contains only digits.

 'ie, characters in the range "0".."9"

 dim iPos

 dim iLen

 

 iLen=len(s)

 for iPos = 1 to iLen

   if not IsDigit(mid(s,iLen,1)) then

     ContainsOnlyDigits = False

     Exit Function

   end if

 next

 

 ContainsOnlyDigits = True

End Function



Function LooksLikeADate(sDate)

 '2004/10/12 ALH

 'This function returns True, if sDate is

 'in the format nn/nn/nnnnn.  No futher checks are applied.

 if (len(sDate)<>10) then

   LooksLikeADate = False

 else

   if mid(sDate,3,1)="/" and mid(sDate,6,1)="/" then

     if ContainsOnlyDigits(mid(sDate,1,2)) and
ContainsOnlyDigits(mid(sDate,4,2)) and ContainsOnlyDigits(mid(sDate,7,4))
then

       LooksLikeADate = True

     else

       LooksLikeADate = False

     end if

   else

     LooksLikeADate = False

   end if

 end if

End Function



Function DaysInMonth(iMonth,iYear)

 '2004/10/12 ALH

 'This function returns the number of days in a month.

 'The year is necessary in order to account for leap years

 'ASSUMPTIONS:  This function uses the Gregorian calendar.

 select case iMonth

   case 2

     if iYear mod 4 = 0 then

       if iYear mod 100 = 0 then

         if iYear mod 400 = 0 then

           DaysInMonth = 29

         else

           DaysInMonth = 28

         end if

       else

         DaysInMonth = 29

       end if

     else

       DaysInMonth = 28

     end if

   case 4,6,9,11

     DaysInMonth = 30

   case else

     DaysInMonth = 31

 end select

End Function



Function BeforeGregorianCalendar(iYear,iMonth,iDay)

 '2004/10/12 ALH

 'This function returns true if the specified

 'year, month, and day, represent a date which would

 'pre-date the introduction of the Gregorian Calendar,

 'on 15 October, 1582.

 if iYear>1582 then

   BeforeGregorianCalendar = False

 elseif iYear<1582 then

   BeforeGregorianCalendar = True

 elseif iMonth>10 then

   BeforeGregorianCalendar = False

 elseif iMonth<10 then

   BeforeGregorianCalendar = True

 elseif iDay>=15 then

   BeforeGregorianCalendar = False

 else

   BeforeGregorianCalendar = True

 end if

End Function



Function DateFromDDMMYYYY(sDate)

 '2004/10/12 ALH

 'This function interprets a string as a date in the format

 'DD/MM/YYYY, regardless of the user's regional settings.

 'If the string cannot be interpreted in this way

 'eg "14/24/1978", then the function will fail silently,

 'and return Null.

 'ASSUMPTIONS:  sDate is in the format "nn/nn/nnnn".

 Dim iYear

 Dim iMonth

 Dim iDay

 Dim iResult

 

 iYear = CInt(mid(sDate,7,4))

 iMonth = CInt(mid(sDate,4,2))

 iDay = CInt(mid(sDate,1,2))



 if iDay>DaysInMonth(iMonth,iYear) then

   DateFromDDMMYYYY = Null

 elseif BeforeGregorianCalendar(iYear,iMonth,iDay) then

   DateFromDDMMYYYY = Null

 else

   DateFromDDMMYYYY = DateSerial(iYear,iMonth,iDay)

 end if

End Function



Function NameOfMonth(iMonth)

 '2004/10/12 ALH

 'This function returns the English name of a month of the year.

 NameOfMonth = trim(mid("January  February March    April    May      June
July     August   SeptemberOctober  November December ",9*(iMonth-1)+1,9))

End Function



Function ErrorMessageFromDDMMYYYY(sDate)

 '2004/10/12 ALH

 'This function returns an appropriate error message

 'to explain why sDate cannot be interpreted as a valid date.

 'ASSUMPTIONS:  Validity of the date has already been checked

 'by calling DateFromDDMMYYYY, and sDate is in the format

 '"nn/nn/nnnn".

 Dim iYear

 Dim iMonth

 Dim iDay

 Dim iDaysInMonth



 iYear = CInt(mid(sDate,7,4))

 iMonth = CInt(mid(sDate,4,2))

 iDay = CInt(mid(sDate,1,2))



 if iMonth>12 or iMonth<1 then

   ErrorMessageFromDDMMYYYY="Please enter a date in the format dd/mm/yyyy."

 elseif BeforeGregorianCalendar(iYear,iMonth,iDay) then

   ErrorMessageFromDDMMYYYY="Please enter a date on or after 15 October,
1582 (the beginning of the Gregorian Calendar)."

 else

   iDaysInMonth = DaysInMonth(iMonth,iYear)

   if iDay = 29 and iMonth = 2 then

     ErrorMessageFromDDMMYYYY=CStr(iYear) & " is not a leap year."

   else

     ErrorMessageFromDDMMYYYY="There are only " & iDaysInMonth & " days in "
& NameOfMonth(iMonth) & "."

   end if

 end if

End Function



Function DateToDDMMYYYY(w)

  '2004/10/12 ALH

  'This function formats a date using the format "dd/mm/yyyy"

  'ASSUMPTIONS:  Year is on or after the introduction of

  'the Gregorian calendar, on 15 October 1582.

  'NOTES:  The Gregorian Calendar was not adopted in England,

  '        until 14 September 1752.  Care should be taken in

  '        accepting dates earlier than this.

  Dim sYear

  Dim sMonth

  Dim sDay

  

  sYear = CStr(Year(w))

  sMonth = CStr(Month(w))

  sDay = CStr(Day(w))

  

  if len(sMonth)=1 then

    sMonth="0" & sMonth

  end if

  

  if len(sDay)=1 then

    sDay="0" & sDay

  end if

  

  DateToDDMMYYYY = sDay & "/" & sMonth & "/" & sYear

End Function



Function ExaminationDate_onblur()



 Set theForm = document.DriverCert

 Dim strdtDate

 Dim wDate



 If ((theForm.ExaminationDate.value)<> "") Then

  strdtDate = (theForm.ExaminationDate.value)

  if Not LooksLikeADate(strdtDate) then

    Alert "Please enter a date in the format dd/mm/yyyy."

    Exit Function

  end if



  wDate = DateFromDDMMYYYY(strdtDate)

  if isnull(wDate) then

    Alert ErrorMessageFromDDMMYYYY(strdtDate)

    Exit Function

  end if



  varDate = DateToDDMMYYYY(wDate)



  theForm.CertificationExpiry.value = varDate



 end if



end Function



</script>

</head>



<body>



<form method="POST" action="--WEBBOT-SELF--" name="DriverCert">

 <!--webbot bot="SaveResults" U-File="_private/form_results.csv"
S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->

 &nbsp;<p align="center">Enter these test values notice with certain dates

 month and day get reversed<br></p>

 <table cellpadding="0" cellspacing="0" width="658" height="73">

  <!-- MSTableType="layout" -->

  <tr>

   <td width="329" height="73" valign="top">date formats
dd/mm/yyyy<p>01/02/2003<br>02/02/2003<br>03/02/2003<br>04/02/2003<br>05/02/2
003<br>06/02/2003<br>07/02/2003<br>08/02/2003<br>09/02/2003<br>

 10/02/2003<br>11/02/2003<br>12/02/2003<br>13/02/2003<br>14/02/2003</td>

   <td width="329" height="73" valign="top">date formats
dd/mm/yyyy<p>01/12/2003<br>02/11/2003<br>03/10/2003<br>04/09/2003<br>05/08/2
003<br>06/07/2003<br>07/06/2003<br>08/05/2003<br>09/04/2003<br>

 10/03/2003<br>11/02/2003<br>12/01/2003<br>&nbsp;</p>

   <p>&nbsp;</td>

  </tr>

 </table>

 <p>&nbsp;enter date here&gt;&gt;<input type="text" name="ExaminationDate"
size="20">&nbsp;

 click here to make onblur occure &gt;&gt;<input type="text"
name="CertificationExpiry" size="20"> </p>

</form>



</body>



</html>







Of course, this example would be better if it were in JavaScript, but that's
a matter of opinion.

Adelle.




[Non-text portions of this message have been removed]



------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/AspClassicAnyQuestionIsOk/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to