In the project I am working on, I need to determine a start and end date in order to get a range of dates. To make my life simple, I am just using a set of drop lists with months and years as options. After the start and end values are collected, I want to set the date range to the 1st day of the month for the start selection and the last day of the month for the end selection.
i.e. August 2014 gives me 08/01/2014 if it is the start and 08/31/2014 if it is the end.
//get the drop lists and the selected values var eYear = document.getElementById('endYearList'); var eYearValue = eYear.options[eYear.selectedIndex].value; var eMonth = document.getElementById('endMonthList'); var eMonthValue = eMonth.options[eMonth.selectedIndex].value; //create the Date object var endDate = new Date(eYearValue,eMonthValue ,1,0,0,0,0); //create the Date object that will provide the Day value var endDateDate = new Date(endDate).setMonth() + 1); //set the Day with the endDateDate endDate.setDate(new Date(endDateDate - 1).getDate)):
It might not be the smallest or best code, but at least I know that it provides the correct value.