-1

I'm using jQuery and want to format a date. I don't want to have to import another plug-in for jQuery. I have found a solution that works in JavaScript i.e.

var now = new Date(); 

var curr_date = d.getDate();

var curr_month = d.getMonth();

curr_month++;

var curr_year = d.getFullYear();

$('##optionButtons').append('<a href="##"class="btn">' 
+ curr_date + "-" + curr_month + "-" 
+ curr_year + '</a> </ br>');

However, when I add the date shown in the example below I get the following error, why?

var d = value.CREATED;

var curr_date = d.getDate();

var curr_month = d.getMonth();

curr_month++;

var curr_year = d.getFullYear();

$('##optionButtons').append('<a href="##">' 
+ curr_date + "-" + curr_month + "-" 
+ curr_year + '</a> </ br>');   

ERROR ##################### d.getDate is not a function ###########################

The value of 'value.CREATED' is October, 25 2012 00:00:00+0000

Prometheus
  • 32,405
  • 54
  • 166
  • 302

3 Answers3

1

I would suggest you to send the date in milliseconds from the server, and then create the date object in javascript as new Date(milliseconds).

See reference here: http://www.w3schools.com/js/js_obj_date.asp

OR

var d = new Date(value.created); //new Date('October, 25 2012 00:00:00+0000')

EDIT: Considering your value.created to be a string, I could parse it in the DEMO here.

I hope this would work.

Pulkit Mittal
  • 5,916
  • 5
  • 21
  • 28
  • the value of 'value.CREATED' is from an ajax call which I cannot change server side. this is the format I have to work with October, 25 2012 00:00:00+0000 – Prometheus Oct 25 '12 at 11:10
  • var d = new Date(value.created); gives me NaN NaN NaN? – Prometheus Oct 25 '12 at 11:16
  • The problem is then with the value.created element. See a running demo http://jsfiddle.net/y6bcR/ – Pulkit Mittal Oct 25 '12 at 11:20
  • Check this post http://stackoverflow.com/questions/2587345/javascript-date-parse. Parse your string first and than pass it into Date.parse. – Andries Oct 25 '12 at 11:24
0

The code: var d = value.CREATED; does not (somehow) return a Date. That's why your call to one of its functions var curr_date = d.getDate(); is illegal. If you show in your example where the variable value come's from, we are maybe able to find the bug.

Andries
  • 1,547
  • 10
  • 29
0

The date format you are using is not valid.

var d = new Date('October, 25 2012 00:00:00');
DDK
  • 1,032
  • 18
  • 39