我正在编写一个API,其中用户只能在变量start_date中传递date(2020-07-14)或DateTime,例如“ 2020-07-14T05:23:51.711Z”,如下所示。如何确保用户仅同时传递了日期或日期时间?
Input1: start_date = "2020-07-14T05:23:51.711Z"
output1: contains date and time both
Input2: start_date = "2020-07-14"
output2: contains only Date
Input3: start_date = "2020-07-14Tsome_random_string"
output3: not a proper DateTime format
Input4: start_date = "some_random_string"
output4: not a proper DateTime format
我已经检查了DateTime API在ruby中没有找到任何特定的内容。
答案 0 :(得分:0)
def valid_date?(str)
date = Date.xmlschema(str)
if(str.include? 'T')
return 'Contains both Date & Time'
end
return 'Contains Date only'
rescue => e
return 'Not a valid Date or DateTime'
end