上传CSV文件时如何将UTC转换为人类可读的格式(How to convert UTC to human readable format when uploading CSV files)

编程入门 行业动态 更新时间:2024-10-26 12:22:44
上传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 end

CSV文件

----------------------------- | 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 end

Controller

def import Product.import(params[:file]) redirect_to root_url end

CSV 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 end

My 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 end

I have considered timestamp number length.

Thanks for participating everyone.

更多推荐

本文发布于:2023-07-21 09:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1209009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   如何将   可读   人类   上传

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!