I am trying to find a format for a string that could be used to instantiate a Date object which would include only a month and a year.
I can't find documentation on what formats of dates are accepted.
Printable View
I am trying to find a format for a string that could be used to instantiate a Date object which would include only a month and a year.
I can't find documentation on what formats of dates are accepted.
You can do this but it will assume current day. Not sure what you are expecting back. This code will produce:
Mon Dec 29 14:16:22 MST 1980
The month in the date object is always minus one.
<script>
var d = new Date();
d.setMonth("11");
d.setYear("1980");
alert(d);
</script>
Here is a link. Just google Javascript Date Object.
http://www.cev.washington.edu/lc/CLW..._datetime.html
Thanks for the example. It is very informative, but I don't quite understand from it how I can pass just a month and a year to a Date object... am I missing something?
check out:
DevGuru
http://www.devguru.com/technologies/...ript/10585.asp
Code:var year=1981;
var month=4;
alert(new Date(year, month))//==Mon Jun 01 1981 00:00:00 GMT-0500 (Central Daylight Time)
As rnd_me showed, giving the date constructor only a year and a month sets the date to the first instant of the first day of that month.
alert(datefrom_ym(2010,4))// use strings or numbersCode:function datefrom_ym(y, m){
return new Date(y, m-1);
}