Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1226
  • Last Modified:

Date Format Convertion European to US

I am very new at this so bear with me. I have located some Javascript that performs a simple date validation. The script, however, is written for the European format (ddmmyy). I tried making a few changes and I got to the point were if I entered 10.31.2003 into the form field onChange would convert it to 10/31/2003. But if I entered the date 10/31/2003 I receive the invalid format error message. What do I need to do to edit this. The following code is in its original format. The function is called using onChange.  

Thanks for the help.

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
function check_date(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = ".";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
        if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
           DateTemp = DateTemp + DateValue.substr(i,1);
        }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = day + seperator + month + seperator + year;
   }
   /* Error-message if err != 0 */
   else {
      alert("Date is incorrect!");
      DateField.select();
        DateField.focus();
   }
}
//  End -->
</script>
0
wtm
Asked:
wtm
1 Solution
 
devicCommented:
hi wtm,
try this:

============================
<SCRIPT LANGUAGE="JavaScript">

<!--// Begin
function check_date(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
  err = 0;
  DateValue = DateField.value;
  /* Delete all chars except 0..9 */
  for (i = 0; i < DateValue.length; i++) {
      if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
         DateTemp = DateTemp + DateValue.substr(i,1);
      }
  }
  DateValue = DateTemp;
  /* Always change date to 8 digits - string*/
  /* if year is entered as 2-digit / always assume 20xx */
  if (DateValue.length == 6) {
     DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
  if (DateValue.length != 8) {
     err = 19;}
  /* year is wrong if year = 0000 */
  year = DateValue.substr(4,4);
  if (year == 0) {
     err = 20;
  }
  /* Validation of month*/
  month = DateValue.substr(0,2);
  if ((month < 1) || (month > 12)) {
     err = 21;
  }
  /* Validation of day*/
  day = DateValue.substr(2,2);
  if (day < 1) {
    err = 22;
  }
  /* Validation leap-year / february / day */
  if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
     leap = 1;
  }
  if ((month == 2) && (leap == 1) && (day > 29)) {
     err = 23;
  }
  if ((month == 2) && (leap != 1) && (day > 28)) {
     err = 24;
  }
  /* Validation of other months */
  if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
     err = 25;
  }
  if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
     err = 26;
  }
  /* if 00 ist entered, no error, deleting the entry */
  if ((day == 0) && (month == 0) && (year == 00)) {
     err = 0; day = ""; month = ""; year = ""; seperator = "";
  }
  /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
  if (err == 0) {
     DateField.value = month + seperator + day + seperator + year;
  }
  /* Error-message if err != 0 */
  else {
     alert("Date is incorrect!");
     DateField.select();
      DateField.focus();
  }
}
//  End -->
</script>
<form>
      <input type=text value="10/22/2003" onblur=check_date(this)>
</form>
0
 
XxavierCommented:
Here is a function that will convert a date to European form to American if it is identifiable as such (had it hanging aound it might be useful)


<script>
function checkdate(date){
 spacer=date.match(/\.|\/|:/)
 dd=date.match(/\d+/)
 date=date.replace(/\d+(\.|\/|:)/,"")
 mm=date.match(/\d+/)
 date=date.replace(/\d+(\.|\/|:)/,"")
 if ((mm > 12) ){return mm+spacer+dd+spacer+date}  
           else {return dd+spacer+mm+spacer+date}
}

document.write('2:14:2002','<br>',checkdate('2:14:2002'),'<p>')
document.write('12/14/2003','<br>',checkdate('12/14/2003'),'<p>')
document.write('10/3/2003','<br>',checkdate('10/3/2003'),'<p>')
document.write('1.3.2003','<br>',checkdate('1.3.2003'),'<p>')
</script>



0
 
ZontarCommented:
My solution is not to let users type in dates in the first place, but to use dropdowns instead, one each for the day, month and year. Makes things much simpler IMO.

Assuming that
(a) you've already got dropdowns, named month, day, and year
(b) the first option in each one is a "dummy" that contains the text "[choose one]" or something similar
(c) the years dropdown starts with the year 2001
<html>
<head>
<script type="text/javascript">
function checkDate(form)
{
  var month = form.month.selectedIndex;
  var day = form.day.selectedIndex;
  var year = form.year.selectedIndex;

  value = true;

  if(year == 0 || month == 0 || day == 0)
  {
    alert("Please select a month, day, and year");    
    value = false;
  }
  else
  {
    year += 1999;
    month -= 1;

    var date = new Date(year, month, day);

    if( date.getFullYear() != year ||  date.getMonth() != month || date.getDate() != day)
    {
      alert("Invalid date.");
      value = false;
    }
  }

  return value;
}
</script>
</head>
<body>
<form action="form-processor.php" onsubmit="return checkDate(this);">
<p>Month: <select name="month">
  <option value="" selected>-- choose one --</option>
  <option value="1">January</option>
  <option value="2">February</option>
  <option value="3">March</option>
<!-- etc. -->
  <option value="12">December</option>
</select></p>

<p>Day: <select name="day">
  <option value="" selected>-- choose one --</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <!-- etc -->
  <option value="29">29</option>
  <option value="30">30</option>
  <option value="31">31</option>
</select></p>

<p>Year: <select name="year">
  <option value="" selected>-- choose one --</option>
  <option value="2000">2000</option>
  <option value="2001">2001</option>
<!-- etc. -->
  <option value="2005">2005</option>
</select></p>
<p><input type="submit" value="submit"></p>
</form>
</body>
</html>

A lot cleaner than mucking about with all those string methods and counting of days and matching days to months and whatnot. :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ZontarCommented:
Note: The script above takes advantage of the fact that the JS Date object automatically rolls over excess days.

For example, suppose the user selects "February 31, 2003" -- the Date constructor automatically converts this to 03 March 2003. Thus, getMonth() retuns 2 (March) and getDate() returns 31, neither which matches what the user entered. The Date object also automatically figures leap years, so 29 February 2004 will pass muster, but 29 February 2003 will result in an error (gets converted to 01 March).

Note that the months for the Date object are numbered 0 - 11, so February = 1, March = 2, etc.

If you decide to start ordering the date correctly as d-m-y (like we do here in Australia), just switch around the month and day dropdowns, everything else can stay the same. ;-)

Oh, and the year dropdown does begin with 2000 and not 2001. Heh.
0
 
ZontarCommented:
Heh, that should have been "Thus, getMonth() returns 2 (March) and getDate() returns 3, neither which matches what the user entered. "

Maybe I should go to bed.
0
 
NetGrooveCommented:
Here my version:


<html>
<head>
<script>
function check_date(field){
 var msg = "";
 var seperator = ".";
 var DateField = field;
 var iDate = DateField.value.split(seperator);
 if(iDate.length!=3){
   msg = "Please enter Date in this format: DD.MM.YYYY";
 } else {
   aDate = new Date(iDate[2],iDate[1]-1,iDate[0]);
   if(aDate.getFullYear()!=iDate[2]||aDate.getMonth()!=iDate[1]-1||aDate.getDate()!=iDate[0]){
     msg = "Wrong Date: "+DateField.value;
   }
 }
 if(msg!=""){
   alert(msg);
   DateField.select();
   DateField.focus();
 } else {
   DateField.value = aDate.getDate() + seperator + (aDate.getMonth()+1) + seperator + aDate.getFullYear();
 }
}
</script>
</head>
<body>
<form>
Date:
<input type=text name=date onChange="check_date(this)">
</form>
</body>
</html>


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now