首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查API响应参数中是否存在特定int。

检查API响应参数中是否存在特定int。
EN

Stack Overflow用户
提问于 2018-10-11 15:12:02
回答 2查看 66关注 0票数 0

在响应参数中,我尝试接受@start_date (它是一个DateTime变量,所以它是一个int),并试图查看它是否在日期中包含了11@start_date输出如下:

代码语言:javascript
复制
2018-11-04 02:00:00 -0600

这就是我试图用的案例陈述。

代码语言:javascript
复制
  #begin case statement to see whether the shift length will go 
  #back/forward
  case
  when (@start_date.include?(11))
    season_logic = (60*60*9)
    puts "The date is a Fall date - The shift will go back one hour"
  when (@start_date.include?(3))
    season_logic = (60*60*7)
    puts "The date is a Spring date - The shift will go forward one hour "
  else
    raise "The season logic could not be calculated"
  end
    season_logic

但是,我说这句话时出错了:

代码语言:javascript
复制
NoMethodError: undefined method 'include?' for # . 
<DateTime:0x007fe5774957f0>
Did you mean?  include
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-11 15:15:17

String#include?这看起来是您想要的,如果您在调用方法调用之前将日期转换为字符串并在字符串上下文中传递'11‘,则可以访问该日期:

代码语言:javascript
复制
    when (@start_date.to_s.include?('11'))
票数 0
EN

Stack Overflow用户

发布于 2018-10-11 18:14:19

如果@start_date是一个DateTime,您可以使用month方法进行比较:

代码语言:javascript
复制
case @start_date.month
when 3
  season_logic = (60*60*7)
  puts "The date is a Spring date - The shift will go forward one hour "
when 11
  season_logic = (60*60*9)
  puts "The date is a Fall date - The shift will go back one hour"
else
  raise "The season logic could not be calculated"
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52763509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档