打印

[技术介绍] 计算两个日期相隔的天数

计算两个日期相隔的天数

#计算两个php?name=%C8%D5%C6%DA" onclick="tagshow(event)" class="t_tag">日期之间相隔的天数时分秒
#Jack 2008年2月22日 11时08分55秒

def SecToDateTime(seconds)
 time = seconds.round     # Get rid of microseconds
 second = time % 60     # Extract seconds
 time /= 60          # Get rid of seconds
 minutes = time % 60     # Extract minutes
 time /= 60          # Get rid of minutes
 hours = time % 24      # Extract hours
 time /= 24          # Get rid of hours
 days = time         # Days (final remainder)
 [days, hours, minutes, second] # Return array [d,h,m,s]
end

t1 = Time.mktime(2008,8,8,20,0,0)
t2 = Time.now
result = SecToDateTime(t1-t2)

puts "距第29届(北京)奥运会开幕:"
printf "%d 天 %d 时 %d 分 %d 秒",result[0],result[1],result[2],result[3]
本帖最近评分记录
  • liumuqing R币 +1 我很赞同 2008-3-19 18:15
  • axgle R币 +2 不错啦 2008-2-25 10:43

TOP

以前忘了在哪看到一个

require 'Date'
(Date.new(year,month,day)..Date.today).to_a.length


话说最早就是为了写这个入了Ruby的门的......
那时候写的那段代码简直不堪入目

TOP



puts Date.parse("2008-8-8")-Date.today


TOP

谢了! 很好用! 有没有任意的日期到任意的日期的相隔的天数呀?
 谢了!

TOP

上面  不好意思我要求的是天数和小时数!

TOP

引用:
原帖由 liupei520520 于 2008-3-19 14:46 发表
谢了! 很好用! 有没有任意的日期到任意的日期的相隔的天数呀?
 谢了!
把Date.today也改成Date.new(year,month,day)不就是两个任意日期间隔的天数了么?

TOP

2008-12-05 05:04 Crawled by CCBot/1.0 (+http://www.commoncrawl.org/bot.html) @38.103.63.61