1

I'm using datepicker for Bootsrap3... also I'm using inputmask so my input looks like this.

<input type="text" class="form-control m_mm_yyyy" name="m_mm_yyyy" id="m_mm_yyyy" required="" data-inputmask="'alias': 'mm/yyyy'" data-mask/>

until that everything is fine, the thing is that using jquery, how do I calculate years and months based on that input information, so rule is that the result from that input must not be lest than 3 months nor higher than 9 years from the current day... I can get an alert or display a div that wont matter the thing is that the form wont be submit until that date is fix with a correct date...
I have try this: https://stackoverflow.com/a/2209104/2293454
and this: https://stackoverflow.com/a/14276675/2293454
but are not working for me, anyone have an idea how to do this? please.

btw I'm using this to trigger the alert or message when the rule happens...

<script>
    $(function() {
        // Masck my Dates as mm/YYYY
        $("#m_mm_yyyy").inputmask("mm/yyyy", {"placeholder": "mm/yyyy"});
        $(".m_mm_yyyy").focusout(function() {
                    // Here goes the rule for getting months and years
                    // var fg = $("#m_mm_yyyy").val();
                    alert(fg); // Working
                    // Now what? ... Have no idea how to calculate months
                    // and year base on that input... 

        })
    });
</script>

Thank you for taking the time.

Community
  • 1
  • 1
Tanker
  • 1,178
  • 3
  • 16
  • 49
  • ***[`Try this`](http://stackoverflow.com/a/23675586/2260614)*** – Bhavik May 28 '14 at 19:50
  • This one seems to be somewhat working for I'm getting invalid date, `var today = new Date(); var format = $(this).val().split("/"); var dob = new Date(format[2], format[1], '1'); var diff = (today - dob); var age = Math.floor(diff / 31536000000); // $('#age').text(age); alert(format);` the output for format is mm/YYYY 02/2014 but the output for DOB is invalid date.. wonder why?... – Tanker May 28 '14 at 20:43

1 Answers1

0

Use moment.js, it has an API to calculate the difference for two dates and return the result in days, months or years.

Link: http://momentjs.com/docs/#/displaying/difference/

TchiYuan
  • 4,258
  • 5
  • 28
  • 35