将时间戳格式保存为日期

时间:2014-10-30 18:35:28

标签: ruby-on-rails

我将Stripe的结算结束期限保存到数据库,默认情况下保存为Epoch时间戳1441292360。如何在cancellation_date列的订阅表中将其保存为YYYY-MM-DD?

订阅模式:

  belongs_to :plan
  belongs_to :subscription
  belongs_to :user

  validates_presence_of :plan_id
  validates_presence_of :email

  def save_with_stripe_payment
    customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
    self.stripe_customer_token = customer.id
    self.cancellation_date = customer.subscriptions.first.current_period_end
    save!
  rescue Stripe::InvalidRequestError => e
    logger.error "Stripe error while creating customer: #{e.message}"
    errors.add :base, "There was a problem with your credit card."
    false
  end

1 个答案:

答案 0 :(得分:1)

如果您有unix时间,可以通过运行Time将其转换为ruby Time.at(unix_time_integer)对象。要转换为日期,您可以运行Time.at(unix_time_integer).to_date