how to compare current date to a future date and validate it in javascript












0















`









`



I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is 2 months greater than the current date. If not, I'd like to display a message to notifying the user the date is less then 2 months but user can still proceed with filling the form after the notification.



Below is the form i want to add the function to.










share|improve this question

























  • I recommend you to use the momentjs.com library, try it. It makes manipulation of dates easy

    – Marcos Pérez Gude
    Dec 3 '15 at 16:03











  • You should check stackoverflow.com/questions/2706125/…

    – Daniel Corzo
    Dec 3 '15 at 16:18
















0















`









`



I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is 2 months greater than the current date. If not, I'd like to display a message to notifying the user the date is less then 2 months but user can still proceed with filling the form after the notification.



Below is the form i want to add the function to.










share|improve this question

























  • I recommend you to use the momentjs.com library, try it. It makes manipulation of dates easy

    – Marcos Pérez Gude
    Dec 3 '15 at 16:03











  • You should check stackoverflow.com/questions/2706125/…

    – Daniel Corzo
    Dec 3 '15 at 16:18














0












0








0








`









`



I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is 2 months greater than the current date. If not, I'd like to display a message to notifying the user the date is less then 2 months but user can still proceed with filling the form after the notification.



Below is the form i want to add the function to.










share|improve this question
















`









`



I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is 2 months greater than the current date. If not, I'd like to display a message to notifying the user the date is less then 2 months but user can still proceed with filling the form after the notification.



Below is the form i want to add the function to.







javascript validation date






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 3 '15 at 17:25







iyke

















asked Dec 3 '15 at 16:02









iykeiyke

113




113













  • I recommend you to use the momentjs.com library, try it. It makes manipulation of dates easy

    – Marcos Pérez Gude
    Dec 3 '15 at 16:03











  • You should check stackoverflow.com/questions/2706125/…

    – Daniel Corzo
    Dec 3 '15 at 16:18



















  • I recommend you to use the momentjs.com library, try it. It makes manipulation of dates easy

    – Marcos Pérez Gude
    Dec 3 '15 at 16:03











  • You should check stackoverflow.com/questions/2706125/…

    – Daniel Corzo
    Dec 3 '15 at 16:18

















I recommend you to use the momentjs.com library, try it. It makes manipulation of dates easy

– Marcos Pérez Gude
Dec 3 '15 at 16:03





I recommend you to use the momentjs.com library, try it. It makes manipulation of dates easy

– Marcos Pérez Gude
Dec 3 '15 at 16:03













You should check stackoverflow.com/questions/2706125/…

– Daniel Corzo
Dec 3 '15 at 16:18





You should check stackoverflow.com/questions/2706125/…

– Daniel Corzo
Dec 3 '15 at 16:18












3 Answers
3






active

oldest

votes


















0














If you used javascript then lets try to this one, It may help you.



<script type="text/javascript">

var date = new Date();
var month = date.getMonth()+1;
var day = date.getDay();
var year = date.getYear();

var newdate = new Date(year,month,day);

mydate=new Date('2011-04-11');

console.log(newdate);
console.log(mydate)

if(newdate > mydate)
{
alert("greater");
}
else
{
alert("smaller")
}


</script>





share|improve this answer
























  • Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it

    – iyke
    Dec 3 '15 at 16:29



















0














If your date is in mm/dd/yyyy format in string then you can use the following method. It will return true if date is 2 months greater than the current date and false otherwise -



 // dateString in "mm/dd/yyyy" string format
function checkMonth(dateString)
{
var enteredMS = new Date(dateString).getTime();
var currentMS = new Date().getTime();
var twoMonthMS = new Date(new Date().setMonth(new Date().getMonth() + 2)).getTime();

if(enteredMS >= twoMonthMS)
{
return true;
}
return false;
}


Invoke this as checkMonth("03/12/2016");






share|improve this answer

































    0

















    var d1 = new Date();
    var d2 = new Date(d1);

    console.log(d1 == d2); // prints false (wrong!)
    console.log(d1 === d2); // prints false (wrong!)
    console.log(d1 != d2); // prints true (wrong!)
    console.log(d1 !== d2); // prints true (wrong!)
    console.log(d1.getTime() === d2.getTime()); // prints true (correct)





    Also you can do <,>,>=,<= etc






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34070628%2fhow-to-compare-current-date-to-a-future-date-and-validate-it-in-javascript%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      If you used javascript then lets try to this one, It may help you.



      <script type="text/javascript">

      var date = new Date();
      var month = date.getMonth()+1;
      var day = date.getDay();
      var year = date.getYear();

      var newdate = new Date(year,month,day);

      mydate=new Date('2011-04-11');

      console.log(newdate);
      console.log(mydate)

      if(newdate > mydate)
      {
      alert("greater");
      }
      else
      {
      alert("smaller")
      }


      </script>





      share|improve this answer
























      • Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it

        – iyke
        Dec 3 '15 at 16:29
















      0














      If you used javascript then lets try to this one, It may help you.



      <script type="text/javascript">

      var date = new Date();
      var month = date.getMonth()+1;
      var day = date.getDay();
      var year = date.getYear();

      var newdate = new Date(year,month,day);

      mydate=new Date('2011-04-11');

      console.log(newdate);
      console.log(mydate)

      if(newdate > mydate)
      {
      alert("greater");
      }
      else
      {
      alert("smaller")
      }


      </script>





      share|improve this answer
























      • Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it

        – iyke
        Dec 3 '15 at 16:29














      0












      0








      0







      If you used javascript then lets try to this one, It may help you.



      <script type="text/javascript">

      var date = new Date();
      var month = date.getMonth()+1;
      var day = date.getDay();
      var year = date.getYear();

      var newdate = new Date(year,month,day);

      mydate=new Date('2011-04-11');

      console.log(newdate);
      console.log(mydate)

      if(newdate > mydate)
      {
      alert("greater");
      }
      else
      {
      alert("smaller")
      }


      </script>





      share|improve this answer













      If you used javascript then lets try to this one, It may help you.



      <script type="text/javascript">

      var date = new Date();
      var month = date.getMonth()+1;
      var day = date.getDay();
      var year = date.getYear();

      var newdate = new Date(year,month,day);

      mydate=new Date('2011-04-11');

      console.log(newdate);
      console.log(mydate)

      if(newdate > mydate)
      {
      alert("greater");
      }
      else
      {
      alert("smaller")
      }


      </script>






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Dec 3 '15 at 16:14









      Afroza YasminAfroza Yasmin

      3621320




      3621320













      • Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it

        – iyke
        Dec 3 '15 at 16:29



















      • Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it

        – iyke
        Dec 3 '15 at 16:29

















      Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it

      – iyke
      Dec 3 '15 at 16:29





      Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it

      – iyke
      Dec 3 '15 at 16:29













      0














      If your date is in mm/dd/yyyy format in string then you can use the following method. It will return true if date is 2 months greater than the current date and false otherwise -



       // dateString in "mm/dd/yyyy" string format
      function checkMonth(dateString)
      {
      var enteredMS = new Date(dateString).getTime();
      var currentMS = new Date().getTime();
      var twoMonthMS = new Date(new Date().setMonth(new Date().getMonth() + 2)).getTime();

      if(enteredMS >= twoMonthMS)
      {
      return true;
      }
      return false;
      }


      Invoke this as checkMonth("03/12/2016");






      share|improve this answer






























        0














        If your date is in mm/dd/yyyy format in string then you can use the following method. It will return true if date is 2 months greater than the current date and false otherwise -



         // dateString in "mm/dd/yyyy" string format
        function checkMonth(dateString)
        {
        var enteredMS = new Date(dateString).getTime();
        var currentMS = new Date().getTime();
        var twoMonthMS = new Date(new Date().setMonth(new Date().getMonth() + 2)).getTime();

        if(enteredMS >= twoMonthMS)
        {
        return true;
        }
        return false;
        }


        Invoke this as checkMonth("03/12/2016");






        share|improve this answer




























          0












          0








          0







          If your date is in mm/dd/yyyy format in string then you can use the following method. It will return true if date is 2 months greater than the current date and false otherwise -



           // dateString in "mm/dd/yyyy" string format
          function checkMonth(dateString)
          {
          var enteredMS = new Date(dateString).getTime();
          var currentMS = new Date().getTime();
          var twoMonthMS = new Date(new Date().setMonth(new Date().getMonth() + 2)).getTime();

          if(enteredMS >= twoMonthMS)
          {
          return true;
          }
          return false;
          }


          Invoke this as checkMonth("03/12/2016");






          share|improve this answer















          If your date is in mm/dd/yyyy format in string then you can use the following method. It will return true if date is 2 months greater than the current date and false otherwise -



           // dateString in "mm/dd/yyyy" string format
          function checkMonth(dateString)
          {
          var enteredMS = new Date(dateString).getTime();
          var currentMS = new Date().getTime();
          var twoMonthMS = new Date(new Date().setMonth(new Date().getMonth() + 2)).getTime();

          if(enteredMS >= twoMonthMS)
          {
          return true;
          }
          return false;
          }


          Invoke this as checkMonth("03/12/2016");







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 3 '15 at 16:23

























          answered Dec 3 '15 at 16:17









          ChandanChandan

          1,028710




          1,028710























              0

















              var d1 = new Date();
              var d2 = new Date(d1);

              console.log(d1 == d2); // prints false (wrong!)
              console.log(d1 === d2); // prints false (wrong!)
              console.log(d1 != d2); // prints true (wrong!)
              console.log(d1 !== d2); // prints true (wrong!)
              console.log(d1.getTime() === d2.getTime()); // prints true (correct)





              Also you can do <,>,>=,<= etc






              share|improve this answer




























                0

















                var d1 = new Date();
                var d2 = new Date(d1);

                console.log(d1 == d2); // prints false (wrong!)
                console.log(d1 === d2); // prints false (wrong!)
                console.log(d1 != d2); // prints true (wrong!)
                console.log(d1 !== d2); // prints true (wrong!)
                console.log(d1.getTime() === d2.getTime()); // prints true (correct)





                Also you can do <,>,>=,<= etc






                share|improve this answer


























                  0












                  0








                  0










                  var d1 = new Date();
                  var d2 = new Date(d1);

                  console.log(d1 == d2); // prints false (wrong!)
                  console.log(d1 === d2); // prints false (wrong!)
                  console.log(d1 != d2); // prints true (wrong!)
                  console.log(d1 !== d2); // prints true (wrong!)
                  console.log(d1.getTime() === d2.getTime()); // prints true (correct)





                  Also you can do <,>,>=,<= etc






                  share|improve this answer
















                  var d1 = new Date();
                  var d2 = new Date(d1);

                  console.log(d1 == d2); // prints false (wrong!)
                  console.log(d1 === d2); // prints false (wrong!)
                  console.log(d1 != d2); // prints true (wrong!)
                  console.log(d1 !== d2); // prints true (wrong!)
                  console.log(d1.getTime() === d2.getTime()); // prints true (correct)





                  Also you can do <,>,>=,<= etc






                  var d1 = new Date();
                  var d2 = new Date(d1);

                  console.log(d1 == d2); // prints false (wrong!)
                  console.log(d1 === d2); // prints false (wrong!)
                  console.log(d1 != d2); // prints true (wrong!)
                  console.log(d1 !== d2); // prints true (wrong!)
                  console.log(d1.getTime() === d2.getTime()); // prints true (correct)





                  var d1 = new Date();
                  var d2 = new Date(d1);

                  console.log(d1 == d2); // prints false (wrong!)
                  console.log(d1 === d2); // prints false (wrong!)
                  console.log(d1 != d2); // prints true (wrong!)
                  console.log(d1 !== d2); // prints true (wrong!)
                  console.log(d1.getTime() === d2.getTime()); // prints true (correct)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 3 '15 at 17:40









                  Basilin JoeBasilin Joe

                  397620




                  397620






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34070628%2fhow-to-compare-current-date-to-a-future-date-and-validate-it-in-javascript%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Hercules Kyvelos

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud