上传CSV文件时如何将UTC转换为人类可读的格式(How to convert UTC to human readable format when uploading CSV files)
上传CSV文件时如何将UTC格式转换为人类可读的格式。
下面是我的模型和控制器,一切正常,CSV文件上传很好。
我需要将UTC转换为人类可读的格式,同时上传CSV文件,这是可能的Ruby on Rails? 我甚至不知道如何做到这一点。
模型
def self.import(file) CSV.foreach(file.path, headers: true) do |row| Product.create! row.to_hash end end调节器
def import Product.import(params[:file]) redirect_to root_url endCSV文件
----------------------------- | name | date_utc | ----------------------------- | John | 13123193563116372 |数据库表
-------------------------------------- | id | name | date_utc | created_at | -------------------------------------- | | | | | --------------------------------------How to convert UTC format to human readable format when uploading CSV files.
Below is my Model & Controller, there are all is OK, CSV files uploading nicely.
I need convert UTC to Human readable format while uploading CSV files, is this possible on Ruby on Rails? I don't know even I don't have any idea how to do this.
Model
def self.import(file) CSV.foreach(file.path, headers: true) do |row| Product.create! row.to_hash end endController
def import Product.import(params[:file]) redirect_to root_url endCSV File
----------------------------- | name | date_utc | ----------------------------- | John | 13123193563116372 |DB Table
-------------------------------------- | id | name | date_utc | created_at | -------------------------------------- | | | | | --------------------------------------最满意答案
这可能有助于你:
CSV.foreach(file.path, headers: true) do |row| product = Product.new product.name = row[0] product.date_utc = Time.at(row[1]) product.save endMy own solution
def self.import(file) CSV.foreach(file.path, headers: true) do |row| p_hash = Product.new p_hash.name = row[0] p_hash.date_utc = Time.at(row[1].to_i).strftime("%B %e, %Y at %I:%M :%S %p") p_hash.save end endI have considered timestamp number length.
Thanks for participating everyone.
更多推荐
发布评论