Link to home
Start Free TrialLog in
Avatar of Moiz Saifuddin
Moiz Saifuddin

asked on

datediff and isdate

can anyone create a Datediff and IsDate function from vbscript to javascript



i mean i want the created or new functions to work like these functions above...




moiz


ASKER CERTIFIED SOLUTION
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
function isDate(value) {
   var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
   var matchArray = value.match(datePat); // is the format ok?
   if (matchArray == null) {
               //alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
               return false;
   }
   return true;
}
Hi,

I've used this function before to validate a date:

function isDate(value)
{
    return !isNaN(new Date(value));
}

Pass it a string and it will try and convert it to a date. If it doesn't it returns false. You need to bear in mind that if your users have a different culture setting on their PC than what you have on your server, then you might not get the right results because the javascript runs on the client side.

So if the user type in in dd/mm/yyyy format a date, for example 01/02/2006 and your server is expecting mm/dd/yyyy then the server wouldn't notice a problem because the user entered the date as the 1st Feb 2006 and the server recorded it is as the 2nd Jan 2006. What I have done more recently is require that the user type the date in in the form dd/MMM/yy where the MMM is the month in a 3 letter code. Then I have put an onblur on the field which calls this function:

function validateDate(oCtrl, psDateString)
{
  var rxDateValidator = /^([012]?\d|3[01])[-\s\/]([Jj][Aa][Nn]|[Ff][Ee][bB]|[Mm][Aa][Rr]|[Aa][Pp][Rr]|[Mm][Aa][Yy]|[Jj][Uu][Nn]|[Jj][u]l|[aA][Uu][gG]|[Ss][eE][pP]|[oO][Cc]|[Nn][oO][Vv]|[Dd][Ee][Cc])[-\s\/]\d\d$/;
  var dtDate = new Date()
 
  if (!(rxDateValidator.test(psDateString) || psDateString == ''))
  {
    alert('The date you entered is invalid!\n\nDates must be entered in the form "dd mmm yy"\neg. 6 Mar 74'); // Lee's birthday
    oCtrl.focus();
    oCtrl.select();
    return false;
  }
}

Not very elegant but it works for me.

Regards,

Lee
Avatar of Moiz Saifuddin
Moiz Saifuddin

ASKER

I tried the isdate function below and it doesnt work as expected....



<html>
<body>
<script language="javascript">
function isDate(value) {
   var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
   var matchArray = value.match(datePat); // is the format ok?
   if (matchArray == null) {
             alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
             return false;
   }
   return true;
}
</script>

<form name="f1">
<input type="button" value="IsDate"
 onclick="isDate (this.form.inpDate1.value);">
 <br>Date 1<input type="text" value="12/05/2005" name="inpDate1">
</form>
</body>
</html>













Here is a complete example using my function.

What are your expectations  of isDate() ?? please explain.


<html>
<body onload="document.f1.inpDate2.focus();">
<script language="javascript">
function isDate(value) {
   var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
   var matchArray = value.match(datePat); // is the format ok?
   if (matchArray == null) {
             alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
             return false;
   }
   return true;
}
</script>

<form name="f1">
 <br>Date 1<input type="text" value="12/05/2005" name="inpDate1">
<input type="button" value="IsDate"
 onclick="isDate (this.form.inpDate1.value);">
 <br>Date 2<input type="text" value="12-05-20" name="inpDate2" onblur="if (!isDate(this.value)) { this.focus();};">
</form>
</body>
</html>
oh sorry,

i want it to work exactly like the IsDate vbscript function. this site below will show yu exactly how i want it


http://www.feri.uni-mb.si/vbscript/vbs131.htm