5

In below Code i want to validate date between 02/08/2017 and 05/08/2017 and give us a alert which is Date is not in range

<script type="text/javascript"> function validate() { today = new Date(); fromdt= new Date("02/08/2017"); todate=new Date("05/08/2017"); if( document.myForm.entrydt.value == "" ) { alert( "Please Select Entry Date!" ); document.myForm.entrydt.focus() ; return false; } else if(!document.myForm.entrydt.value.match(letters3)) { alert("Entry Date: Enter Only Date Format i.e DD/MM/YYYY"); document.myForm.entrydt.focus() ; return false; } else if (!document.myForm.entrydt.value.today > startdt && !document.myForm.entrydt.value.today < todate) { alert("Entry Date: Enter Date in Proper Range"); document.myForm.entrydt.focus() ; return false; } return( true ); } </script> 

==============================================================================

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="form-inline" role="form" name="myForm" id="myForm" onsubmit="return(validate());"> <div id="" class="container" > <div class="form-group"> <label for="entrydt">Entry Date</label>= <input class="form-control" style='font-weight:bold;text-transform:uppercase;' id="entrydt" type="text" name="entrydt" style='' placeholder="DD/MM/YYYY" value="" size="10"> </div> </div> </form> 
3
  • Check the documentation for the Date constructor to see the accepted formats. Also remember that Date actually includes both date and time, so you'll need to take that into accounting by for the end of your range. Commented Aug 2, 2017 at 5:55
  • And your problem is? Commented Aug 2, 2017 at 5:55
  • Note that new Date("02/08/2017") will probably be parsed as 8 Feb 2017. It is not clear from your post wether it should be treated as that or 2 Aug 2017. See marked duplicates. Commented Aug 2, 2017 at 6:11

2 Answers 2

7

new Date().getTime() will give time in milliseconds as long value number to compare values

var today = new Date().getTime(); // 1501653935994 var from = new Date("02/08/2017").getTime(); // gives 1486492200000 var to = new Date("05/08/2017").getTime(); if(today >= from && today <= to) { // your code goes here } 
Sign up to request clarification or add additional context in comments.

3 Comments

You are relying on implementation dependent parsing. Use of getTime is unnecessary.
but sir how we write in this sytax is else if (!document.myForm.entrydt.value.today > startdt && !document.myForm.entrydt.value.today < todate)
@KirankumarGajananGawade then you should convert the input value to a Date before the comparison: var inputDate = new Date(document.myForm.entrydt.value.today). You might want to put a try/catch around that in case the date is not entered in a valid format. In fact, you should give the input type="date" and take advantage of the built-in datepicker and validation.
5
new Date().getTime() 

gives you the timestamp in ms.

var today = new Date().getTime(); var from = new Date("02/08/2017").getTime(); var to = new Date("05/08/2017").getTime(); var withinRange = today >= from && today <= to; 

5 Comments

you can use an excellent Javascript library : moment.js that provides helpers to compare dates. Just set a moment var : var mmtDate = moment(new Date()); Set comparison date : var theDate = moment('2017-08-15'); Finally use methods : var before = theDate.isBefore(mmtDate) ? true : false; Hope it helps
Thanks for the hint - but why don´t you post it as an answer?
@FranzDeschler—because posting "use library X" is not an answer and parsing strings without providing the format is error prone.
but sir how we write in this sytax is else if (!document.myForm.entrydt.value.today > startdt && !document.myForm.entrydt.value.today < todate)
(!(new Date().getTime() > startdt) && !(new Date().getTime() < todate))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.