#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    Boulder, CO
    Posts
    17
    Rep Power
    0

    Javascript Date Object


    I've coded the following Javascript to parse a date and the alert shows a valid date (dte.toString()) but each of the y, m, d variables shows NaN.

    var display_msg;
    var y, m, d, dte;
    dte = new Date(obj.value) obj.value = "Mar 12, 2003"
    strdte = dte.toString();
    y = dte.getFullYear();
    m = dte.getMonth();
    d = dte.getDate();
    display_msg = "Dte - " + strdte;
    display_msg += "\ny - " + y;
    display_msg += "\nm - " + m;
    display_msg += "\nd - " + d;
    alert(display_msg);


    When I subsitute a current date now = new Date() the y, m, and d display fine?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    24
    Don't know where you're getting you're obj.value from:

    var display_msg;
    var y, m, d, dte;
    value = "Mar 12, 2003";
    dte = new Date(value);

    strdte = dte.toString();
    y = dte.getFullYear();
    m = dte.getMonth();
    d = dte.getDate();
    display_msg = "Dte - " + strdte;
    display_msg += "\ny - " + y;
    display_msg += "\nm - " + m;
    display_msg += "\nd - " + d;
    alert(display_msg);

    works fine!
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    Boulder, CO
    Posts
    17
    Rep Power
    0
    Well I did it again. The issue was my coding of an if statement rather than the date code.


    I coded

    if(NaN(dte)
    {
    y = dte.getFullYear();
    m = dte.getMonth();
    ...
    }
    else
    {
    y = substr(....

    }

    The if(NaN) should have been if(!NaN) thats' why the NaN showed in the display_msg.

    It's always the little things.

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo