rails 日期Date、时间Time的一些常用方法总结

获取当前时间、今天的当前日期

  • Time.now 
  • 国际时间就是Time.now.utc
  • Date.today

日相关的获取方法

  • 一天的开始也就是0点00分
2.4.1 :001 > Time.now.beginning_of_day
 => 2018-08-05 00:00:00 +0800

或者:

2.4.1 :006 > Time.now.at_beginning_of_day
 => 2018-08-05 00:00:00 +0800
  • 一天的结束也就是23点59分
2.4.1 :008 > Time.now.at_end_of_day
 => 2018-08-05 23:59:59 +0800

或者:

2.4.1 :009 > Time.now.end_of_day
 => 2018-08-05 23:59:59 +0800
  • 厉害的来了,表示一天的时间段 从0点0分~~23点59分,笨方法是:
2.4.1 :010 > Time.now.beginning_of_day..Time.now.end_of_day
 => 2018-08-05 00:00:00 +0800..2018-08-05 23:59:59 +0800

或者聪明点的是

2.4.1 :011 > Time.now.all_day
 => 2018-08-05 00:00:00 +0800..2018-08-05 23:59:59 +0800
  • 同理:当前一周,对、聪明,都会强答了
2.4.1 :012 > Time.now.all_week
 => 2018-07-30 00:00:00 +0800..2018-08-05 23:59:59 +0800
  • 我们做查询是通常用的就是时间范围之类的,比如、获取前几天时间
2.4.1 :013 > Time.now + 2.day
 => 2018-08-07 16:32:38 +0800
2.4.1 :014 > Time.now - 2.day
 => 2018-08-03 16:32:43 +0800
2.4.1 :015 > Time.now - 3.minute
 => 2018-08-05 16:30:19 +0800
  • 日期也是同理
2.4.1 :026 > Date.today
 => Sun, 05 Aug 2018
2.4.1 :027 > Date.yesterday
 => Sat, 04 Aug 2018
2.4.1 :028 > Date.today + 3.day
 => Wed, 08 Aug 2018

在rails console中、查看一个类的所有方法。使用

Time.now.methods

2.4.1 :005 > Time.now.methods
 => [:ago, :beginning_of_day, :midnight, :at_midnight, :at_beginning_of_day, :middle_of_day, :midday, :noon, :at_midday, :at_noon, :at_middle_of_day, :at_end_of_day, :beginning_of_hour, :at_beginning_of_hour, :end_of_hour, :at_end_of_hour, :beginning_of_minute, :at_beginning_of_minute,
 :end_of_minute, :at_end_of_minute, :plus_with_duration, :plus_without_duration, :minus_with_duration, :minus_without_duration, :minus_with_coercion, :minus_without_coercion, :compare_with_coercion, :eql_with_coercion, :eql_without_coercion, :to_datetime, :formatted_offset, :+, :-, :as_json, :ctime, :to_formatted_s, :round, :to_default_s, :to_time, :blank?,
 :<=>, :acts_like_time?, :change, :eql?, :zone, :in, :utc, :to_r, :localtime, :gmtime, :getlocal, :getgm, :getutc, :min, :sec, :inspect, :asctime, :day, :hour, :mday, :year, :mon, :wday, :isdst, :dst?, :gmtoff, :gmt_offset,
 :utc_offset, :utc?, :gmt?, :sunday?, :monday?, :succ, :tuesday?, :month, :friday?, :tv_usec, :saturday?, :thursday?, :tv_nsec, :nsec, :yday,
 :strftime, :subsec, :to_a, :to_s, :to_i, :httpdate, :sec_fraction, :tv_sec, :wednesday?, :rfc822, :to_date, :xmlschema, :usec, :to_f, :iso8601, :compare_without_coercion, :rfc3339, :rfc2822, :hash, :seconds_since_midnight, :seconds_until_end_of_day, :end_of_day, :advance, :since,
 :preserve_timezone, :next_day, :prev_day, :next_month, :prev_month, :next_year, :prev_year, :beginning_of_week, :tomorrow, :yesterday, :past?, :today?, :future?, :monday, :sunday, :on_weekend?, :on_weekday?, :days_ago, :days_since, :weeks_ago, :weeks_since, :months_ago, :months_since, :years_ago, :years_since, :beginning_of_month, :at_beginning_of_month, :beginning_of_quarter, :at_beginning_of_quarter, :end_of_quarter, :end_of_month,
 :at_end_of_quarter, :beginning_of_year, :at_beginning_of_year, :next_week, :next_weekday, :next_quarter, :prev_week, :last_week, :prev_weekday, :last_weekday, :last_month, :prev_quarter, :last_quarter, :last_year, :days_to_week_start, :at_beginning_of_week, :end_of_week, :at_end_of_week,
 :at_end_of_month, :end_of_year, :at_end_of_year, :all_day, :all_week, :all_month, :all_quarter, :all_year, :in_time_zone, :<, :>, :<=, :>=, :==, :between?, :clamp, :to_json, :`, :to_yaml, :to_yaml_properties, :presence, :present?, :psych_to_yaml, :acts_like?, :to_param, :to_query, :deep_dup, :duplicable?,
 :in?, :presence_in, :instance_values, :instance_variable_names, :with_options, :html_safe?, :pry, :__binding__, :require_dependency,
 :unloadable, :require_or_load, :load_dependency, :pretty_print, :pretty_print_cycle, :pretty_print_instance_variables, :pretty_print_inspect, :try, :try!, :instance_of?, :kind_of?, :is_a?, :tap, :public_send, :method, :public_method, :singleton_method, :remove_instance_variable,
 :class_eval, :define_singleton_method, :instance_variable_set, :extend, :pretty_inspect, :to_enum, :enum_for, :gem, :===, :=~, :!~, :respond_to?, :suppress_warnings, :freeze, :object_id, :send,
 :byebug, :display, :debugger, :remote_byebug, :nil?, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods,
 :private_methods, :public_methods, :instance_variable_get, :instance_variables, :instance_variable_defined?, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]

猜你喜欢

转载自blog.csdn.net/weixin_42428631/article/details/80876157