简体   繁体   English

Rails:从DateTime字段显示不是00:00或12:00的时间

[英]Rails: Show time when it's not 00:00 or 12:00 from a DateTime field

In my rails app I have a datetime field, which defaults to 12:00 when time is not specified. 在我的Rails应用程序中,我有一个datetime字段,如果未指定时间,则默认为12:00。 I only intend to display time is not 00:00 or 12:00, if it's 00:00 then I only want to display date and not time. 我只打算显示时间不是00:00或12:00,如果是00:00,那么我只想显示日期而不是时间。 It there's time other than 12:00 or 00:00 then I want to display both. 除了12:00或00:00以外,我想同时显示两者。

You can do this, 你可以这样做,

time = datetime_field.strftime("%I:%M")
if time == '12:00'
  # do something
else
  # do something else
end

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails-Postgresql更新datetime给我正确的日期,但是时间是00:00:00 - Rails - Postgresql updating datetime leaves me with correct date but time is 00:00:00 Rails ActiveRecord时间在10:00-00:00之间 - Rails ActiveRecord time between 10:00 - 00:00 Rails将json渲染到AngularJS前端。 时间变为00:00:00 - Rails render json to AngularJS frontend. Time becomes 00:00:00 如何在时间对象中转换此日期格式? “ 20111210000000 +0100”-> 2011-12-12 00:00 - How to convert this date format in a Time object? “20111210000000 +0100” -> 2011-12-12 00:00 Rails应用程序中的时间字段全部填充为2000年1月1日上午12:00 - Time fields in Rails app all getting populated with January 1, 2000, 12:00AM 如何处理“ datetime”值,以便将其从“ 2012-04-27 00:00:00”更改为“ 2012年4月27日”? - How to handle a `datetime` value so to change it from `2012-04-27 00:00:00` to `27 april 2012`? 计算开始时间为 09:00 AM 的日历天 Rails - Calculate calendar days Rails where start time is 09:00 AM 带allow_blank的可选time_select默认为00:00 - Optional time_select with allow_blank defaults to 00:00 to_formatted_s(:long_ordinal)最后返回00:00 - to_formatted_s(:long_ordinal) returning 00:00 at the end 将2016-12-28 14:14:00 UTC日期时间格式转换为rails应用程序中的12/06/2016 11:28 PM格式 - Convert 2016-12-28 14:14:00 UTC date time format to 12/06/2016 11:28 PM format in rails application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM