我如何得到日期落在视角的日子? [英] How can i get the dates to fall on the perspective day?

查看:179
本文介绍了我如何得到日期落在视角的日子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得每天的日期/号码?这是我的代码如下:

  class Month 
attr_reader:month,:year

def initialize(month,year)
@month = month
@year = year
end

def month_names
names_of_months = {1 = 'January',2 => 'February',3 => 'March',4 => 'April',5 => 'May',6 => 'June',7 => 'July',8 => 'August',9 => 'September',10 => '10月',11 => 'November',12 => 'December'}
return names_of_months [@month]
end

def length
days_of_months = {1 => 31,2 => 28,3 => 31,4 => 30,5 => 31,6 => 30,7 => 31,8 => 31,9 => 30,10 => 31,11 => 30,12 => 31}
return days_of_months [@month]
end

def to_s
weekdays =Su Mo Tu We Th Fr Sa
month =# {month_names}#{year}
output = [
month.center(weekdays.size),
weekdays
] .join(\\\

(1..length).each do | day |
output<< day.to_s
end
输出
end
end

以下是我的结果。

  2017年1月
Su Mo Tu We Th Fr Sa12345678910111213141516171819202122232425262728293031


解决方案

由于您的问题已被诊断,我将限制我的答案,请使用日期类。



代码

  require'date'
DAY_WIDTH = 4

def calendar(year,month)
title =#{Date :: MONTHNAMES [month]},#{year}。center(7 * DAY_WIDTH )
puts\\\
#{title}
Date :: ABBR_DAYNAMES.each {| s | print s.rjust(DAY_WIDTH)}
puts
arr = [* [''] * Date.new(year,month).wday,* 1..days_in_month(year,month)]
arr.each_slice(7){| week |
week.each {| d | print d.to_s.rjust(DAY_WIDTH)}; puts}
end

def days_in_month(year,month)
((month == 12)?Date.new(year + 1,1):Date.new year,month + 1)) -
Date.new(year,month))
end



示例

 日历(2015,2)

2015年2月
周日周二周二周三周四周五周六周六周六周六周六周日13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28

日历(2015,4)

2015年4月
星期一星期二星期三星期四星期五星期六
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

日历(2016,2)

2016年2月
周日周二周二周三周四周五周六周六周六周六1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29



说明



示例:

  year = 2015 
month = 4

首先打印标题:

  title =#{Date :: MONTHNAMES [month]},#{year}。center(7 * DAY_WIDTH)
#=> April,2015.center(28)
#=> April,2015
puts\\\
#{title}

 日期:: ABBR_DAYNAMES.each {| s | print s.rjust(DAY_WIDTH)} 
#=> [Sun,Mon,Tue,Wed,Thu,Fri,Sat]。
#=> print s.rjust(4)}
#星期一星期二星期三星期四星期五星期六星期六星期六星期二星期二星期二星期二星期二星期二星期二星期二星期二星期二星期二星期二星期二星期二星期二星期二星期二星期三星期二例如,Sun.rjust(4)#=>Sun



打印每周的日期我做了两个步骤:首先创建一个阵列天打印,包括 0-6 在第一周的空间,然后打印每组七个元素:

  arr = [* [''] * Date.new(year,month).wday ,* 1..days_in_month(year,month)] 
#=> arr = [* [''] * 3,* 1..30]
#=& ','',''],* 1..30]
#=> [,,,1,2,...,30] code>

我们现在将 arr 分成七组, p>

  arr.each_slice(7){| week | 
week.each {| d | print d.to_s.rjust DAY_WIDTH)}; puts}

例如:

 '.to_s.d.to_s.rjust(4)
#=>''
10.to_s.rjust b#=>'10'

2015年4月的天数计算如下:

 ((month == 12)? Date.new(year + 1,1):Date.new(year,month + 1)) -  
Date.new(year,month))
#=> ((4 == 12)?Date.new(2016,1):Date.new(2015,5)) - Date.new(2015.4)
#=> Date.new 5) - Date.new(2015,4)
#=>#< Date:2015-05-01((2457144j,0s,0n),+ 0s,2299161j)> -
#< Date:2015-04-01((2457114j,0s,0n),+ 0s,2299161j)>
#=>(30/1),等于30的有理数


How can i get the day/number go under each day? This is my code below:

class Month
  attr_reader :month, :year

  def initialize( month, year)
    @month = month
    @year = year
  end

  def month_names
    names_of_months = {1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December'}
    return names_of_months[@month]
  end

  def length
   days_of_months  = {1 => 31, 2 => 28, 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, 8 => 31, 9 => 30, 10 => 31, 11 => 30, 12 => 31}
   return days_of_months[@month]
  end

  def to_s
    weekdays = "Su Mo Tu We Th Fr Sa"
    month    = "#{month_names} #{year}"
    output   = [
       month.center(weekdays.size),
       weekdays
    ].join("\n")
    (1..length).each do |day|
      output << day.to_s
    end
    output
  end
end

Below is my result.

January 2017
Su Mo Tu We Th Fr Sa12345678910111213141516171819202122232425262728293031

解决方案

As your problem has been diagnosed, I will limit my answer to showing how you could make use of the class Date.

Code

require 'date'
DAY_WIDTH = 4

def calendar(year, month)
  title = "#{Date::MONTHNAMES[month]}, #{year}".center(7*DAY_WIDTH)
  puts "\n#{title}"
  Date::ABBR_DAYNAMES.each { |s| print s.rjust(DAY_WIDTH) }
  puts
  arr = [*[' ']*Date.new(year,month).wday, *1..days_in_month(year,month)]
  arr.each_slice(7) { |week|
    week.each { |d| print d.to_s.rjust(DAY_WIDTH) }; puts }
end

def days_in_month(year,month)
  (((month==12) ? Date.new(year+1,1) : Date.new(year,month+1)) -
    Date.new(year,month))
end

[Edit: In his answer here, @spickerman expressed the number of days in the month thus: Date.new(year,month,-1).day. Better,eh?]

Examples

calendar(2015, 2)

       February, 2015       
 Sun Mon Tue Wed Thu Fri Sat
   1   2   3   4   5   6   7
   8   9  10  11  12  13  14
  15  16  17  18  19  20  21
  22  23  24  25  26  27  28

calendar(2015, 4)

        April, 2015         
 Sun Mon Tue Wed Thu Fri Sat
               1   2   3   4
   5   6   7   8   9  10  11
  12  13  14  15  16  17  18
  19  20  21  22  23  24  25
  26  27  28  29  30

calendar(2016, 2)

       February, 2016       
 Sun Mon Tue Wed Thu Fri Sat
       1   2   3   4   5   6
   7   8   9  10  11  12  13
  14  15  16  17  18  19  20
  21  22  23  24  25  26  27
  28  29

Explanation

Consider the second example:

year = 2015
month = 4

First print the title:

title = "#{Date::MONTHNAMES[month]}, #{year}".center(7*DAY_WIDTH)
  #=> "April, 2015".center(28)
  #=> "        April, 2015         " 
puts "\n#{title}"

Next print the day of week header:

Date::ABBR_DAYNAMES.each { |s| print s.rjust(DAY_WIDTH) }
  #=> ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"].each { |s|
  #=>    print s.rjust(4) }
  #   Sun Mon Tue Wed Thu Fri Sat

For example, "Sun".rjust(4)" #=> " Sun".

Now we need to print the days in each week. I've done that in two steps: first create an array days to be printed, including 0-6 spaces in the first week, then print each group of seven elements:

  arr = [*[' ']*Date.new(year,month).wday, *1..days_in_month(year,month)]
    #=> arr = [*[' ']*3, *1..30]
    #=> arr = [*[' ', ' ', ' '], *1..30]
    #=> [" ", " ", " ", 1, 2,..., 30] 

We now divide arr into groups of seven and print each:

arr.each_slice(7) { |week|
  week.each { |d| print d.to_s.rjust(DAY_WIDTH) }; puts }

For example:

' '.to_s.d.to_s.rjust(4)
   #=> '    ' 
10.to_s.rjust(4)
   #=> '  10'

The number of days in April, 2015 is computed as follows:

(((month==12) ? Date.new(year+1,1) : Date.new(year,month+1))- 
    Date.new(year,month))
  #=> (((4==12) ? Date.new(2016,1) : Date.new(2015,5))-Date.new(2015,4)
  #=> Date.new(2015,5) - Date.new(2015,4)
  #=> #<Date: 2015-05-01 ((2457144j,0s,0n),+0s,2299161j)> -
  #<Date: 2015-04-01 ((2457114j,0s,0n),+0s,2299161j)>
  #=> (30/1), a rational number equal to 30

这篇关于我如何得到日期落在视角的日子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆