`

通过年和周的参数获取本周始末日期

    博客分类:
  • RUBY
 
阅读更多
实现通过年和周的参数获取本周始末日期。(其中每年1月1号,所在的周会算在去年中去。也就是本年第一周从本年第一个周一开始)

def zhou(y,z)
  da=y.to_s
  firstdata=da+"-01-01"#本年的一月一号
  tian=z*7+(8-firstdata.to_time.wday) #N周就有N*7天,再加上每年1月1号是周几的差天
  mon=0
     for i in 1..12#每年12个月
       mon=mon+1 #处于第几个月
       mont_date=days_in_month(i, year = y)#本年本月有几天
       tian=tian-mont_date
         if tian<0||tian==0 #如果剩余天数比i月天数少
         tian=tian+mont_date #则不减断开
         break
         end
     end
   da=da+"-"+mon.to_s+"-"+tian.to_s #年+月+日(字符串通过to_time变为时间格式天)
   @start_time=da.to_time.beginning_of_week.to_formatted_s(:db) #本天所在周始
   @end_time=da.to_time.end_of_week.to_formatted_s(:db)#本周周末
 end
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics