Skip to content

Instantly share code, notes, and snippets.

@setkyar
Last active August 29, 2015 14:04
Show Gist options
  • Save setkyar/49e222a24d1daa627a03 to your computer and use it in GitHub Desktop.
Save setkyar/49e222a24d1daa627a03 to your computer and use it in GitHub Desktop.
JavaScript Date Notes
## [To get first and last day of the month in js](http://stackoverflow.com/questions/13571700/get-first-and-last-date-of-current-month-with-javascript-or-jquery)
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
## For Month Select
function dateDropdown(select) {
var today = new Date();
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
var month = today.getMonth();
for (var i = month; i < months.length; i++) {
select.options[select.options.length] = new Option(months[i] + ' '+ today.getFullYear(),months[i]);
};
// for (i in months) {
// select.options[select.options.length] = new Option(months[i] + ' '+ today.getFullYear(),months[i]);
// };
// select.options[today.getMonth()] = new Option(months[today.getMonth()]+ ' '+ today.getFullYear(), months[today.getMonth()], true, true) //select today's month
today.setFullYear(today.getFullYear() + 1);
for (i in months) {
if (months[i] == months[today.getMonth()]) { break; };
select.options[select.options.length] = new Option(months[i] + ' '+ today.getFullYear(),months[i]);
};
}
dateDropdown(document.getElementById('your-element-id'));
@setkyar
Copy link
Author

setkyar commented Aug 5, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment