Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder StealthRT's Avatar
    Join Date
    Aug 2006
    Posts
    185
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Question FormatDate error

    Hey all i am getting an error when trying to use the following code:
    Code:
    <script language="javascript">
    	var theDate = 'Fri May 15 00:00:00 EDT 2009';
    	
    	var curr_date = theDate.getDate();
    	var curr_month = theDate.getMonth();
    	var curr_year = theDate.getFullYear();
    	
    	alert(curr_date + ' ' + curr_month + ' ' + curr_year);
    </script>
    the Error is on the "var curr_date = theDate.getDate();" part. What i am trying to get is just the day, month and year out of the string in theDate. So this is what i am looking to get IF the code worked as-is:
    Code:
    15 5 2009
    Any help for this problem would be awesome!

    Thanks,
    David

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    You are invoking Date methods on theDate, but in your code theDate is not a Date object but a string.

    Replace
    Code:
    var theDate = 'Fri May 15 00:00:00 EDT 2009';
    with
    Code:
    var theDate = new Date();
    theDate.setTime(Date.parse('Fri May 15 00:00:00 EDT 2009'));
    and it will work.

  • #3
    Regular Coder StealthRT's Avatar
    Join Date
    Aug 2006
    Posts
    185
    Thanks
    14
    Thanked 0 Times in 0 Posts
    venegal: thanks for the reply. However, what i am using formats it like i have in the string theDate. Its a jQuery calender program and that's how it formats the dates for each day. In other words, i cant change the format, i just have to find a way to get the info i need out of the theDate.

    David

  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Well did you do what I told you to do? You can change the format from a string to a Date object; that's exactly what the snippet I gave you does.

  • #5
    Regular Coder StealthRT's Avatar
    Join Date
    Aug 2006
    Posts
    185
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Oh wow, i didnt even see that you did that! That worked just fine. Thanks!

    David


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •