Ruby构造日期对象和计算日期间天数差的问题

Python025

Ruby构造日期对象和计算日期间天数差的问题,第1张

我写了个程序 你看一下 不明白的话联系我 有注释

#获取当前的时间

today = Time.new

puts "当前日期:" + today.strftime("%Y-%m-%d %H:%M:%S")#采用格式化输出

#获取指定日期的时间 如 2010-08-30 20:50:01

day = Time.local(2010, 8 , 30 , 20, 50, 1)

puts "指定日期:" + day.strftime("%Y-%m-%d %H:%M:%S")

#获取指定日期的秒数(就是1970年到指定日期的秒数)

sec1 = day.to_i

#同理,获取今天的秒数

sec2 = today.to_i

#秒数求差,同时折合为天数

num = (sec1 - sec2)/(24*60*60)

puts (day.strftime("%Y-%m-%d %H:%M:%S") + "与" + today.strftime("%Y-%m-%d %H:%M:%S") + "相差" + num.to_s + "天")

Net::HTTP.new方法,返回resp码和实际的data:

require 'net/http'  

h = Net::HTTP.new("www.baidu.com",80)  

resp,data = h.get("/")   

puts resp  

puts data