View Full Version : FormatDate error

05-23-2009, 11:08 AM
Hey all i am getting an error when trying to use the following 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);

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:

15 5 2009

Any help for this problem would be awesome! :)


05-23-2009, 12:37 PM
You are invoking Date methods on theDate, but in your code theDate is not a Date object but a string.


var theDate = 'Fri May 15 00:00:00 EDT 2009';

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

05-23-2009, 07:32 PM
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.


05-24-2009, 01:50 AM
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.

05-25-2009, 10:15 AM
Oh wow, i didnt even see that you did that! That worked just fine. Thanks! :)