将Meetup API中的“time”转换为可识别格式时出现语法错误

时间:2014-10-16 17:30:41

标签: ruby-on-rails json api meetup

使用下面的代码,我将Meetup事件保存到我的数据库,但由于Meetup的时间格式而无法节省时间。

还有另外两个StackOverflow问题可以解决这个问题,但在尝试实现它们时遇到了语法错误。任何建议将不胜感激!

  1. Converting milliseconds to a date (jQuery/JS)

  2. How do I convert the "time" from the Meetup API to a recognizable format?

  3. Meetup事件时间格式:

    不转换时间戳的代码:

    require 'rubygems'
    require 'json'
    require 'net/http'
    
    class MeetupController < ApplicationController
    
        respond_to :json
        $meetupRI = "http://api.meetup.com/2/open_events?status=upcoming&radius=25.0&category=2&and_text=False&limited_events=False&desc=False&offset=0&photo-host=public&format=json&zip=02903&page=20&sig_id=MYKEY"
    
        def getJobs
            if response.code == '200' then
    
                response = Net::HTTP.get_response(URI.parse($meetupRI))
                data = response.body
                parsed_response = JSON.parse(data)
                parsed_response["results"].each do |event|
                    e = Event.new(:name => event["name"], :description => event["description"], :url => event["event_url"], :start_time => event["time"] )
                    e.save
                end
            end
        end
    end
    

1 个答案:

答案 0 :(得分:1)

我将它变成这样的日期对象:

DateTime.strptime(event['time'].to_s,'%Q')