1

I am trying to use jquery/js code that has the user select there birth month then calculate to the next birth month (from today)? I am new to javascript and can not figure out this date stuff.. I keep getting a invalid date error I want the user to select a month and it to subtract from now().. to figure out how many months are left until there next birthday. Not worrying about days or years just dealing with months.. Any suggestions?

get to the next birth month (from today)?

<select name="month1" id="month1" onchange="date()" size="1">
    <option value="0">January</option>
    <option value="1">February</option>
    <option value="2">March</option>
    <option value="3">April</option>
    <option value="4">May</option>
    <option value="5">June</option>
    <option value="6">July</option>
    <option value="7">August</option>
    <option value="8">September</option>
    <option value="9">October</option>
    <option value="10">November</option>
    <option value="11">December</option>
</select>



<script language="JavaScript">
    function date() {
        var birthmonth = new Date(month1.value);
        var today = new Date();
    }
    document.write("From "+birthmonth+" to "+today+" is "+datediff(birthmonth,today)+" day(s)<br>");
    </script>
  • Read the [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) for `Date`. The month is 0-based, not 1-based. So January should be 0, not 1. – Matt Burland Oct 17 '14 at 19:39
  • And you can't construct a date from just a month value. If you give it a single parameter, it will be interpreted as milliseconds epoch time. Again - read the docs. – Matt Burland Oct 17 '14 at 19:40
  • Also, you define `birthmonth` and `today` inside the function `date` as local variables. They are not accessible outside the function unless you return them or use a variable defined outside the function. – Matt Burland Oct 17 '14 at 19:43
  • thanks for the help this is confusing –  Oct 17 '14 at 19:46
  • I don't understand how you only grab the month when you use date ughhh –  Oct 17 '14 at 19:46
  • why are you converting month value to a date in the first place if all you want is to compare months? – charlietfl Oct 17 '14 at 19:54
  • @charlietfl Looks like he's trying to get difference in days, not months – wired_in Oct 17 '14 at 19:59

2 Answers2

4

Use the getter and setter methods of the Date object. So:

var d = new Date();
// today's month
var month = d.getMonth();
// set the month to January
d.setMonth(0);

Here's a very basic example of calculating a difference based on selected value (using jQuery 1.11):

http://jsfiddle.net/moj94ppe/

Update: A revised version calculating the months to go to arrive at the birth month.

http://jsfiddle.net/moj94ppe/1/

Update: A revised version to incorporate options.

http://jsfiddle.net/moj94ppe/4/

Leo Bedrosian
  • 3,336
  • 2
  • 16
  • 22
2

I create this to calculate the difference:

function date(obj) {
  var birthmonthDate = new Date();
  var birthmonth = new Date(birthmonthDate.setMonth(parseInt(obj.value, 10) - 1, 1));
  var today = new Date();
  console.log("From " + birthmonth + " to " + today + " is " + DateDiff.inDays(today, birthmonth) + " day(s)<br>");
}

var DateDiff = {

  inDays: function(d1, d2) {
    var t2 = d2.getTime();
    var t1 = d1.getTime();

    return parseInt((t2 - t1) / (24 * 3600 * 1000));
  },

  inWeeks: function(d1, d2) {
    var t2 = d2.getTime();
    var t1 = d1.getTime();

    return parseInt((t2 - t1) / (24 * 3600 * 1000 * 7));
  },

  inMonths: function(d1, d2) {
    var d1Y = d1.getFullYear();
    var d2Y = d2.getFullYear();
    var d1M = d1.getMonth();
    var d2M = d2.getMonth();

    return (d2M + 12 * d2Y) - (d1M + 12 * d1Y);
  },

  inYears: function(d1, d2) {
    return d2.getFullYear() - d1.getFullYear();
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="month" id="month" onchange="date(this)" size="1">
  <option value="01">January</option>
  <option value="02">February</option>
  <option value="03">March</option>
  <option value="04">April</option>
  <option value="05">May</option>
  <option value="06">June</option>
  <option value="07">July</option>
  <option value="08">August</option>
  <option value="09">September</option>
  <option value="10">October</option>
  <option value="11">November</option>
  <option value="12">December</option>
</select>
<br>

You can pass this(this refers to the DOM element) in your function to get the value of selected month.

References

How to calculate date difference in javascript

Community
  • 1
  • 1
Alex Char
  • 31,748
  • 8
  • 48
  • 67
  • snippet isn't doing anything? –  Oct 17 '14 at 20:02
  • 1
    Snippet using console.log. Open your console with `f12` and you see the result :) Also is better to avoid document.write. – Alex Char Oct 17 '14 at 20:02
  • Poor example for a snippet should have just used `document.write()` seriously what is the big deal with `console.log()`? Go back a few years browsers didn't even have an inspect window!! – user692942 Oct 19 '14 at 08:29
  • Take a look here [Why is document.write considered a “bad practice”?](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) – Alex Char Oct 19 '14 at 11:47