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

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

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

module TimeFormat

  def self.time_text new_time=Time.now,old_time=Time.now
    if new_time.year == old_time.year
      hour_subtract = new_time.to_i/3600 - old_time.to_i/3600
      min_subtract = new_time.to_i/60 - old_time.to_i/60
      sec_subtract = new_time.to_i - old_time.to_i
      if new_time.at_beginning_of_day == old_time.at_beginning_of_day
        if min_subtract<60
          if sec_subtract<60
            "#{sec_subtract}秒前"
          else
            "#{min_subtract}分钟前"
          end
        else
          "#{hour_subtract}小时前"
        end
      else
        if (old_time.at_beginning_of_day+1.day) == new_time.at_beginning_of_day
          "1天前"
        else
          old_time.strftime("%m月%d日")
        end
      end
    else
      old_time.strftime("%Y年%m月%d日")
    end
  end
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值