在时间的运算上,也常常使用到日期格式的转换,如日期字符串转为日期型,日期转为格式化字符串,是两种常见的需求;另外也有需要将时间转为时间戳的场景等等;
时间数据的转换上,主要用to_date、to_timestamp,即可满足日期字符串转为日期型的需求;
from_timestamp、from_unixtime,即可满足日期转为格式化字符串的需求;
unix_timestamp,可以满足具有时间戳要求的场景;
再有其他场景大家可以根据需要,看看下面的方法有没有你所需要的哈
方法说明:
| 序号 | 语法类型/方法名称 | 输出类型 | 使用说明 |
| 1 | to_date(timestamp date) | string | 返回时间戳对应的date |
| 2 | to_timestamp(bigint unixtime) | timestamp | 返回整数对应的timestamp值 |
| 3 | to_timestamp(string date,string pattern) | timestamp | 返回字符串对应的timestamp值 |
| 4 | to_utc_timestamp(timestamp t,string timezone) | timestamp | 指定时区的时间戳转化为UTC时区的时间戳 |
| 5 | from_timestamp(timestamp t,string pattern) | string | 把timestamp按照pattern进行格式化 |
| 6 | from_timestamp(string date,string pattern) | string | 把date按照pattern进行格式化 |
| 7 | from_unixtime(bigint unixtime) | string | 把时间戳秒数转化为本地地区中的字符串 |
| 8 | from_unixtime(bigint unixtime,string pattern) | string | 时间戳转化为本地时区字符串,pattern格式 |
| 9 | from_utc_timestamp(timestamp t,string timezone) | timestamp | UTC时区指定时间戳转化为指定时区时间戳 |
| 10 | unix_timestamp(string datetime) | bigint | 把string类型的date或日期转化成时间戳Unix |
| 11 | unix_timestamp(timestamp datetime) | bigint | 把string类型的timestamp转化成时间戳Unix |
| 12 | unix_timestamp(string datetime,string pattern) | bigint | 日期按pattern转化成时间戳Unix |
方法示例:
| 序号 | 语法类型/方法名称 | 输出结果 |
| 1 | select now() | 2022-11-10 14:22:36.927 |
| 2 | select to_date(now()) | 2022-11-10 |
| 3 | select to_timestamp(1668089883) | 2022-11-10 14:18:03.000 |
| 4 | select to_timestamp('2022/11/10','yyyy/MM/dd') | 2022-11-10 00:00:00.000 |
| 5 | select to_utc_timestamp(now(),'Asia/Shanghai') | 2022-11-10 06:23:36.036 |
| 6 | select from_timestamp(now(),'yyyy/MM') | 2022/11 |
| 7 | select from_timestamp('2022-11-10','yyyy/MM') | 2022/11 |
| 8 | select from_unixtime(1668089883) | 2022-11-10 14:18:03 |
| 9 | select from_unixtime(1668089883,'yyyy/MM') | 2022/11 |
| 10 | select from_utc_timestamp(now(),'Asia/Shanghai') | 2022-11-10 22:24:25.055 |
| 11 | select unix_timestamp('2022-11-01') | 1667260800 |
| 12 | select unix_timestamp(now()) | 1668090279 |
| 13 | select unix_timestamp('2022-11-01 14:26:27','yyyy-MM-dd HH:mm:ss') | 1667312787 |
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)