Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Calculating age in years, months and days is a bit trickier than it should be due to the differences in month and year lengths. Here's a function that will return the difference between two dates in years, months, days, hours, minutes and seconds.</p> <pre><code>function dateDifference(start, end) { // Copy date objects so don't modify originals var s = new Date(+start); var e = new Date(+end); var timeDiff, years, months, days, hours, minutes, seconds; // Get estimate of year difference years = e.getFullYear() - s.getFullYear(); // Add difference to start, if greater than end, remove one year // Note start from restored start date as adding and subtracting years // may not be symetric s.setFullYear(s.getFullYear() + years); if (s &gt; e) { --years; s = new Date(+start); s.setFullYear(s.getFullYear() + years); } // Get estimate of months months = e.getMonth() - s.getMonth(); months += months &lt; 0? 12 : 0; // Add difference to start, adjust if greater s.setMonth(s.getMonth() + months); if (s &gt; e) { --months; s = new Date(+start); s.setFullYear(s.getFullYear() + years); s.setMonth(s.getMonth() + months); } // Get remaining time difference, round to next full second timeDiff = (e - s + 999) / 1e3 | 0; days = timeDiff / 8.64e4 | 0; hours = (timeDiff % 8.64e4) / 3.6e3 | 0; minutes = (timeDiff % 3.6e3) / 6e1 | 0; seconds = timeDiff % 6e1; return [years, months, days, hours, minutes, seconds]; } </code></pre> <p>You can abbreviate the above just after the year part and return just that if you want.</p> <p>Note that in your code:</p> <pre><code>var cmbmonth=parseInt(document.getElementById("cmbmonth").options[document.getElementById("cmbmonth").selectedIndex].value); </code></pre> <p>can be:</p> <pre><code>var cmbmonth = document.getElementById("cmbmonth").value; </code></pre> <p>There is no need for <em>parseInt</em>, the Date constructor will happily work with string values. If you have used calendar month numbers for the values (i.e. Jan = 1) then subtract 1 before giving it to the Date constructor, but simpler to use javascript month indexes for the values (i.e. Jan = 0).</p> <p>You can then do:</p> <pre><code>var diff = dateDifference(new Date(cmbyear, cmbmonth, cmbdate), new Date()); if (diff[0] &lt; 18) { // sorry, under 18 } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload