Link to home
Start Free TrialLog in
Avatar of pandian
pandian

asked on

date manipulation in javascript

I want to know date manipulation in javascript.
1. how to compare current date with the user date entry.
2. how to add current date with the string format.
For example "dd/mm/yyyy"
usrdate = "25/02/2000"
if( curdate > usrdate )

give some examples

thank you
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

The easiest is to not let the user make mistakes:
<FORM>
<SELECT NAME="theYear">
<OPTION VALUE="">Select a year
<OPTION VALUE="2000">2000
<OPTION VALUE="2001">2001
<OPTION VALUE="2002">2002
<OPTION VALUE="2003">2003
</SELECT>
<SELECT NAME="theMonth">
<OPTION VALUE="">Select a month
<OPTION VALUE="0">1
<OPTION VALUE="1">2
<OPTION VALUE="2">3
<OPTION VALUE="3">4
..
..
<OPTION VALUE="11">12
</SELECT>
<SELECT NAME="theDay">
<OPTION VALUE="">Select a day
<OPTION VALUE="1">1
<OPTION VALUE="2">2
<OPTION VALUE="3">3
<OPTION VALUE="4">4
..
..
<OPTION VALUE="31">31
</SELECT>
<input type="button" onClick="compare(this.form)" VALUE="compare">
and then use

<script>
/* Compare and calculate (c) 2000 Michel Plungjan */
function compare(theForm)
   theYear = theForm.theYear.options[theForm.theYear.selectedIndex].value;
   theMonth = theForm.theMonth.options[theForm.theMonth.selectedIndex].value;
   theDay = theForm.theMonth.options[theForm.theMonth.selectedIndex].value;

   userDate = new Date(theYear,theMonth,theDay);

   testYear = userDate.getYear();
   if (testYear<1000)testYear +=1900;
   if (testYear != theYear ||
userDate.getMonth() != theMonth ||
userDate.getDay() != theDay) {
   alert('Date is not correct');
   return false;

   now = new Date();
   if (userDate.getTime() < now.getTime()) alert('Date must be later than today');
}
</script>

I apologise for any typo.

Michel
PS: You might want to change to this:

function compare(theForm)
                                     theYear = theForm.theYear.options[theForm.theYear.selectedIndex].value;
                                     theMonth = theForm.theMonth.options[theForm.theMonth.selectedIndex].value;

                                     theDay = theForm.theMonth.options[theForm.theMonth.selectedIndex].value;
   if (theYear = '' || theMonth = '' ||theDay = '') {
   alert('Please select year and month and day');
   return false;
   }


and have a return true at the end of the function:

   return true;
}
</script>
ASKER CERTIFIED SOLUTION
Avatar of xabi
xabi

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
Avatar of pandian
pandian

ASKER

Thank you.

I am using  perl program to genarate the HTML with the embeded javascript. There I have to create multiple same data entry form. That each form on same the page should have three date entry field execpt others. So I can not use the option control. May be it takes more space on the page and loading time. so I leave it for text box.
Any way thanks for your coding. I will get some knowladge from you script. Keeping that I will mannage.

Thank you one again.

Xabi, are you poaching???

Michel
Sorry michael, as you know I'm Spanish and my English is far from perfect. What's poaching? :)

xabi
Answering a question where a comment might be the answer
(poaching is when people go hunting where they are not allowed)

Michel
Ok, sorry. It was a crossposting. Sorry.

xabi