Ruby 无效年份的日期验证
我试着做一些感觉很基本的事情,但很挣扎。我希望能够确定一个日期是否有效,如果有效,则解析它并以格式化字符串输出,否则返回“坏日期”。这就是我到目前为止所取得的成就Ruby 无效年份的日期验证,ruby,jruby,Ruby,Jruby,我试着做一些感觉很基本的事情,但很挣扎。我希望能够确定一个日期是否有效,如果有效,则解析它并以格式化字符串输出,否则返回“坏日期”。这就是我到目前为止所取得的成就 require 'date' date_last_printed = current_item.getProperties['Last Printed'] begin d = DateTime.parse(date_last_printed.to_s) if Date.valid_date?(d.year, d.mo
require 'date'
date_last_printed = current_item.getProperties['Last Printed']
begin
d = DateTime.parse(date_last_printed.to_s)
if Date.valid_date?(d.year, d.month, d.day)
d.strftime("%d/%m/%Y %H:%M:%S")
else
'bad date'
end
rescue Exception => ex
ex.message
end
但是01/01/1601
将作为有效日期返回
如何调整代码段以仅返回有效日期(Unix划时代开始时或之后)。您可以在begin语句中执行划时代转换
begin
raise if (t=Time.parse(date_last_printed)).to_i < 0
t.strftime("%d/%m/%Y %H:%M:%S")
rescue
'bad date'
end
开始
将if(t=Time.parse(上次打印的日期))提升到i<0
t、 strftime(“%d/%m/%Y%H:%m:%S”)
营救
“糟糕的约会”
结束
实际上MSWindows也使用1-1-1601,我不知道。每天都是上学的日子!根据你的评论,我稍微改变了我的问题。A)将此转化为你可以调用的方法。B) 实际打印输出。c)检查年份>1970。为什么你会考虑<代码> 01/01/1601 < /代码>无效日期?你对有效日期的规定是什么?