Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rails在where()中将datetime转换为date_Date_Ruby On Rails 4_Where Clause - Fatal编程技术网

Rails在where()中将datetime转换为date

Rails在where()中将datetime转换为date,date,ruby-on-rails-4,where-clause,Date,Ruby On Rails 4,Where Clause,我有以下问题: 我需要将where select中的日期时间字段转换为日期字段 scope :after_start_date, lambda { |value| where('end_time <= (?)', value) if value} scope:在开始日期之后,lambda{| value | where('end|u time您可以使用ruby方法创建自己的日期/时间显示 time.strftime(字符串)=>string %a - The abbreviated

我有以下问题:

我需要将where select中的日期时间字段转换为日期字段

scope :after_start_date, lambda { |value| where('end_time <= (?)', value) if value}
scope:在开始日期之后,lambda{| value | where('end|u time您可以使用ruby方法创建自己的日期/时间显示

time.strftime(字符串)=>string

  %a - The abbreviated weekday name (``Sun'')
  %A - The  full  weekday  name (``Sunday'')
  %b - The abbreviated month name (``Jan'')
  %B - The  full  month  name (``January'')
  %c - The preferred local date and time representation
  %d - Day of the month (01..31)
  %H - Hour of the day, 24-hour clock (00..23)
  %I - Hour of the day, 12-hour clock (01..12)
  %j - Day of the year (001..366)
  %m - Month of the year (01..12)
  %M - Minute of the hour (00..59)
  %p - Meridian indicator (``AM''  or  ``PM'')
  %S - Second of the minute (00..60)
  %U - Week  number  of the current year,
          starting with the first Sunday as the first
          day of the first week (00..53)
  %W - Week  number  of the current year,
          starting with the first Monday as the first
          day of the first week (00..53)
  %w - Day of the week (Sunday is 0, 0..6)
  %x - Preferred representation for the date alone, no time
  %X - Preferred representation for the time alone, no date
  %y - Year without a century (00..99)
  %Y - Year with century
  %Z - Time zone name
  %% - Literal ``%'' character

   t = Time.now
   t.strftime("Printed on %m/%d/%Y")   #=> "Printed on 04/09/2003"
   t.strftime("at %I:%M%p")            #=> "at 08:56AM"
例如:

date = Date.parse('2014-09-08 00:00:00 UTC')
# => Mon, 08 Sep 2014
date.strftime('%d-%m-%Y')
# => "08-09-2014"
并相应地在数据库中搜索..我希望这可能对您有所帮助

编辑:
如果您想从SQL Server获取数据并将datetime转换为date,那么我更新了我的问题描述。因此,我希望问题更清楚。@user3726033:那么您的意思是要将DB存储数据的datetime转换为date???@user3726033:检查我的答案中的编辑部分…如果它对您有帮助,您可以进行升级投票并接受;)