admin管理员组

文章数量:1323194

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?

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?

Share Improve this question edited Nov 29, 2011 at 12:01 lucapette 20.7k6 gold badges67 silver badges59 bronze badges asked Nov 29, 2011 at 11:33 JeffJeff 953 silver badges8 bronze badges 4
  • In what language do you want to do the conversion? What is the relevance of UTC for dates? – RobG Commented Nov 29, 2011 at 11:36
  • What is f.date? A string or a ruby date object? – Mark Commented Nov 29, 2011 at 20:38
  • The primary use is for Highcharts, so the date conversion needs to be in javascript...but if there is a way to convert in ruby in a model first, then render to js, that works. – Jeff Commented Nov 30, 2011 at 6:54
  • f.date is the column in the order table for when each order is entered...it could be called anything (ex: "purchased_at") – Jeff Commented Nov 30, 2011 at 6:56
Add a ment  | 

1 Answer 1

Reset to default 10

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.

本文标签: Railsconvert date string into javascript date object for highchartsStack Overflow