ruby 关于返回几秒前,几小时前,1天前......

这个需求是:
当小于60秒的时候返回时间为xx秒前;
当小于60分钟大于60秒的时候返回xxx小时前;
当隔1天的时候显示一天前;
当大于隔2天的时候,显示xx月xx日;
当跨年的时候显示xxxx年xx月xx日;

下面是实现代码,具体的话你可以按照你的需求进行修改

module TimeFormatdef self.time_text new_time=Time.now,old_time=Time.nowif new_time.year == old_time.yearhour_subtract = new_time.to_i/3600 - old_time.to_i/3600min_subtract = new_time.to_i/60 - old_time.to_i/60sec_subtract = new_time.to_i - old_time.to_iif new_time.at_beginning_of_day == old_time.at_beginning_of_dayif min_subtract<60if sec_subtract<60"#{sec_subtract}秒前"else"#{min_subtract}分钟前"endelse"#{hour_subtract}小时前"endelseif (old_time.at_beginning_of_day+1.day) == new_time.at_beginning_of_day"1天前"elseold_time.strftime("%m月%d日")endendelseold_time.strftime("%Y年%m月%d日")endend
end


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部