Reputation: 134
I have a select field with multiple answer possibilities ranging from 2000 to 2050. So if someone chooses 2013, I need the function to give me a variable with the amount of years passed since 2013.
I tried searching for the code but i didn't find any that fit my need. Thanks to all of you in advance.
Upvotes: 1
Views: 44
Reputation: 333
You can use the javascript date object. https://www.w3schools.com/js/js_date_methods.asp
var date = new Date(); //create date object
var year = date.getFullYear(); //get the year from date object
var enteredYear = parseInt("2000"); //get the value from the select
var diff = year - enteredYear;
/* for absolute value use:
var diff = Math.abs(year - enteredYear);
*/
//do something with it!!
alert('The difference is ' + diff + ' years');
Output The difference is 17 years (it is currently 2017)
Good Luck!! Hope this helps you!!
Upvotes: 3