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
pandianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michel PlungjanIT ExpertCommented:
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
0
Michel PlungjanIT ExpertCommented:
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>
0
xabiCommented:
Here is how to compare dates:

--------- 8< ---------- 8< -----
<html>
<head>
<title>Example</title>
</head>
<script>
<!--
function compare(mydate) {
  var date = new Date(mydate.substring(6,10), mydate.substring(3,5)-1, mydate.substring(0,2))
  var now = new Date();
  var today = new Date(now.getYear(),now.getMonth(),now.getDate());
  if (date > today) alert("Day: " + mydate + " is the future")
  else if (date < today) alert("Day: " + mydate + " is the pass")
  else alert("Day: " + mydate + " is the today")
}
-->
</script>
<body>
  <a href="javascript:compare('13/01/2000')">Press</a>
</body>
</html>
------- 8< ------- 8< --------

I can't understand what you want to do in question 2. Please explain with example.

xabi
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

pandianAuthor Commented:
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.

0
Michel PlungjanIT ExpertCommented:
Xabi, are you poaching???

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

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

Michel
0
xabiCommented:
Ok, sorry. It was a crossposting. Sorry.

xabi
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.