Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - convert date string into javascript date object for highcharts

I'm working on a dashboard app using Rails 3.1.1/Highcharts with several graphs, but am stuck on converting a date string from the rails model into a javascript format that highcharts can accept.

My sample code:

<%= @orders.map{ |f| [f.date, f.units.to_f] }.inspect %>

Produces this output:

[[Fri, 04 Nov 2011, 182.0], [Sun, 06 Nov 2011, 189.0], [Tue, 08 Nov 2011, 178.0], [Thu, 10 Nov 2011, 115.0], [Sat, 12 Nov 2011, 135.0], [Mon, 14 Nov 2011, 120.0], [Thu, 17 Nov 2011, 181.0], [Sun, 20 Nov 2011, 145.0]]

I need to have the date format of the array as follows:

Date.UTC(2010, 10, 4).

Any suggestions on an existing rails/ruby method or how to create a helper?

like image 926
Jeff Avatar asked Nov 29 '11 11:11

Jeff


1 Answers

This probably isn't the best way to do this, but I've been doing:

data: <%= @orders.map{ |f| [f.date.to_datetime.to_i, pdd.field_5x5 ] }.inspect %>,

...which expresses the date in seconds.

like image 77
Mike Kijewski Avatar answered Nov 01 '22 02:11

Mike Kijewski