Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting String to Datetime Rails

I'm using LazyHighCharts and trying to convert json data to display only the last 24hrs, I'm having some troubles converting the date and time ("2014-06-16 16:00:00") to milliseconds.

data structure

{"status": "ok", "data": [{"2014-06-16 16:00:00": 24.2},{"2014-06-17 12:00:00": 30.2},{"2014-06-18 17:00:00": 42.9}]} etc

Controller

@data = JSON.parse(open(@temperature.url).read)

dates = []
temps = []

@data['data'].each do |data|
 dates << data.keys
 temps << data.values
end 

datetime = dates.each do |d| DateTime.parse(d).to_i end

@graph = LazyHighCharts::HighChart.new('graph') do |f|
 f.chart(:height => '400')
 f.yAxis [:title => {:text => "Temperature", :margin => 20, style: { color: '#333'}}]
 f.series(:pointInterval => 1.hour, :pointStart => 30.day.ago, :type => 'area', :name => '24hrs', :data => [[datetime, temps]])
 f.options[:xAxis] = { :minTickInterval => 24 * 3600 * 1000, :type => "datetime", :dateTimeLabelFormats => { day: "%b %e"}, :title => { :text => nil }, :labels => { :enabled => true } }
end
like image 601
DollarChills Avatar asked Aug 19 '14 23:08

DollarChills


1 Answers

You need to covert sstring to dateTime as the first, Use this code:

DateTime.parse("2011-05-19 10:30:14").strftime('%Q')

Or this code:

"2014-06-16 16:00:00".to_datetime.strftime('%Q')

So you can convert array of strings of dates as the following:

dates.map!{|d| d.to_datetime.strftime('%Q')}

Helper links: link-1, link-2

like image 81
Mohamed Yakout Avatar answered Sep 19 '22 02:09

Mohamed Yakout