Cornelius Wilson
Cornelius Wilson

Reputation: 2914

Saving timestamp format as a Date

I am saving Stripe's billing end period to the database and by default it saves as Epoch timestamp 1441292360. How can I save it as YYYY-MM-DD in the Subscriptions table for the cancellation_date column?

Subscription model:

  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

Upvotes: 1

Views: 714

Answers (1)

ptd
ptd

Reputation: 3053

If you have unix time, you can convert it to a ruby Time object by running Time.at(unix_time_integer). To do a conversion to a date, you could run Time.at(unix_time_integer).to_date.

Upvotes: 4

Related Questions