Ruby on rails Rails-确定一周中的哪一天

Ruby on rails Rails-确定一周中的哪一天,ruby-on-rails,ruby,datetime,dayofweek,weekday,Ruby On Rails,Ruby,Datetime,Dayofweek,Weekday,因此DateTime.current返回Fri,2016年3月11日19:34:10+0000 如何确定一周中的哪一天。例如,如果DateTime.current是星期五(只是一周中的一天,与日期无关) DateTime.current==DateTime.parse(“星期五”)不起作用,因为DateTime.parse(“星期五”)返回的2016年3月11日星期五00:00:00+0000不一样 如何检查仅日期或仅时间是否等于特定值 提前谢谢 基本上我想看看DateTime.current既

因此
DateTime.current
返回
Fri,2016年3月11日19:34:10+0000

如何确定一周中的哪一天。例如,如果
DateTime.current
是星期五(只是一周中的一天,与日期无关)

DateTime.current==DateTime.parse(“星期五”)
不起作用,因为
DateTime.parse(“星期五”)
返回的
2016年3月11日星期五00:00:00+0000
不一样

如何检查仅日期或仅时间是否等于特定值

提前谢谢


基本上我想看看DateTime.current既不是周末也不是公共假日,而是在办公时间之间。在Ruby 2.1.1中,Date类有一个
friday?
方法

如果日期是星期五,则返回true

首先,需要
日期

require 'date'
然后使用当前日期创建一个新的日期实例。这里有一个例子

current_time = Time.now
year = current_time.year
month = current_time.month
day = current_time.day

date = Date.new(year, month, day)
date.friday?
=> true
根据您的编码偏好,您可能会更加干燥

date = Date.new(Time.now.year, Time.now.month, Time.now.day)
=> #<Date: 2016-03-11 ((2457459j,0s,0n),+0s,2299161j)>
date.friday?
此外,如果您在工作时间工作,那么使用
business\u time
gem可能是最简单的

您还可以将
假日
gem与
业务时间
一起包括在内

首先,安装gems

gem install business_time
gem install holidays
那么需要宝石吗

require 'business_time'
require 'holidays'
看看今天是不是工作日

Date.today.workday?
这是一个假期

Holidays.on(date, :us).empty?
您现在可以使用类似的方法来确定今天是否是假日

Holidays.on(date, :us).empty?
而且是在上班时间之间

办公时间的定义因人而异。没有一成不变的答案。但是,使用
business\u time
gem,您可以设置配置

BusinessTime::Config.beginning_of_workday = "8:30 am"
BusinessTime::Config.end_of_workday = "5:30 pm"
来源

红宝石

宝石

检查此项-

require 'date'
today = DateTime.current.to_date

if today.Friday?
  puts "Today is a Friday!"
end

您可以使用以下内容:

day = DateTime.now.strftime("%A")
这将返回全名为大写的星期几。您也可以这样获得缩写:

day = DateTime.now.strftime("%a")
您还可以使用%u或%w将一周中的某一天作为整数获取。星期五是5点

DateTime.now.strftime("%u") == 5 # True if Friday
您可以在此处查看更多功能:


谢谢,但我需要从头开始编写,还需要比较两个DateTime对象,所以布尔值在这里对我没有帮助!你说从头开始写是什么意思?