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


JavaScript

Avatar of undefined
Last Comment
Moiz Saifuddin

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Pravin Asar

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Pravin Asar

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;
}
Lee

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
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>













Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Pravin Asar

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>
Moiz Saifuddin

ASKER
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