user4143057
user4143057

Reputation:

jQuery/Javascript month date subtract from current month

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>

Upvotes: 1

Views: 5063

Answers (2)

Leo Bedrosian
Leo Bedrosian

Reputation: 3799

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/

Upvotes: 4

Alex Char
Alex Char

Reputation: 33228

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

Upvotes: 2

Related Questions