Date object acceptable strings
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?
Data Object - date formats
javawebdog
Two things to remember:
"The only place success comes before work is in the dictionary."
"It's more than just a matter of survival. It's a matter of sympathy, compassion, passion and style."
Code:
var year=1981;
var month=4;
alert(new Date(year, month))//==Mon Jun 01 1981 00:00:00 GMT-0500 (Central Daylight Time)
Originally Posted by
harrierdh
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
If you do that, it will fail when the current month has 31 days and the specified has less.
Eric
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.
Code:
function datefrom_ym(y, m){
return new Date(y, m-1);
}
alert(datefrom_ym(2010,4))// use strings or numbers
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks