Ruby时间算法

在 Ruby 中,我们可以用时间做一些简单的算术。

案例

时间运算

Ruby 时间运算

#!/usr/bin/ruby -w # -*- coding : utf-8 -*- puts "HaiCoder(www.haicoder.net)" now = Time.now # 当前时间 puts now past = now - 10 # 10 秒之前。Time - number => Time puts past future = now + 10 # 从现在开始 10 秒之后。Time + number => Time puts future diff = future - now # => 10 Time - Time => 秒数 puts diff

程序运行后,控制台输出如下:

07_Ruby时间运算.png

使用加减实现时间的运算。

Ruby时间算法总结

在 Ruby 中,我们可以用时间做一些简单的算术。