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>
RobG
  • 142,382
  • 31
  • 172
  • 209
  • 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. – jcaron Aug 02 '17 at 05:55
  • And your problem is? – Ataur Rahman Munna Aug 02 '17 at 05: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. – RobG Aug 02 '17 at 06:11

2 Answers2

6

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
}
Selvakumar
  • 527
  • 2
  • 13
  • You are relying on implementation dependent parsing. Use of *getTime* is unnecessary. – RobG Aug 02 '17 at 06:12
  • but sir how we write in this sytax is else if (!document.myForm.entrydt.value.today > startdt && !document.myForm.entrydt.value.today < todate) – Kirankumar Gajanan Gawade Aug 02 '17 at 07:20
  • @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. – Joe Maffei Aug 09 '21 at 13:39
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;
Franz Deschler
  • 2,456
  • 5
  • 25
  • 39
  • 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 – Jean-Luc Aubert Aug 02 '17 at 06:06
  • Thanks for the hint - but why don´t you post it as an answer? – Franz Deschler Aug 02 '17 at 06:08
  • @FranzDeschler—because posting "use library X" is not an answer and parsing strings without providing the format is error prone. – RobG Aug 02 '17 at 06:12
  • but sir how we write in this sytax is else if (!document.myForm.entrydt.value.today > startdt && !document.myForm.entrydt.value.today < todate) – Kirankumar Gajanan Gawade Aug 02 '17 at 07:20
  • (!(new Date().getTime() > startdt) && !(new Date().getTime() < todate)) – Selvakumar Aug 02 '17 at 08:17