IPhone Cases - Debt Consolidation - Debt Consolidation - Moroccan Property - Deaf Topics

PDA

View Full Version : javscript month selection


jon.london
Aug 15th 2007, 12:58 am
Hi guys,

I need to be able to fill a HTML select field with the previous 12 months (including todays' month) to now, including the year. So if I was to load my page today, i would want to see the following values in my select field:

aug-2007
jul-2007
jun-2007
may-2007
apr-2007
mar-2007
feb-2007
jan-2007
dec-2006
nov-2006
oct-2006
sep-2006

How can I do this using Javascript?

Many thanks,

jon

mjamesb
Aug 19th 2007, 11:48 am
<body onload="buildDlist()">

<select id="mydates"></select>

<script language="javascript">

function buildDlist()
{
var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var mylist = document.getElementById("mydates");
var d=new Date()

var opt = new Option(m_names[d.getMonth()] + "-" + d.getFullYear(), m_names[d.getMonth()] + "-" + d.getFullYear());
mylist.options[mylist.options.length] = opt;

for (var i=0; i<11; i++)
{
d.setMonth(d.getMonth() - 1);
var opt = new Option(m_names[d.getMonth()] + "-" + d.getFullYear(), m_names[d.getMonth()] + "-" + d.getFullYear());
mylist.options[mylist.options.length] = opt;
}
}
</script>

</body>