www.webdeveloper.com
Results 1 to 7 of 7

Thread: Date object acceptable strings

Hybrid View

  1. #1
    Join Date
    Sep 2009
    Posts
    51

    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.

  2. #2
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    104
    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

  3. #3
    Join Date
    Sep 2009
    Posts
    51
    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?

  4. #4
    Join Date
    Nov 2008
    Location
    Akron, OH
    Posts
    233

    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."

  5. #5
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    Quote Originally Posted by harrierdh View Post
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Code:
    var year=1981;
    var month=4;
    
    alert(new Date(year, month))//==Mon Jun 01 1981 00:00:00 GMT-0500 (Central Daylight Time)

  7. #7
    Join Date
    Feb 2006
    Posts
    2,927
    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
  •  
HTML5 Development Center



Recent Articles