如何获取DD/MM/YYYY HH:MM
格式的当前日期和时间并递增月份?
发布于 2011-09-14 20:05:18
可以像这样进行格式化(我以为您指的是HH:MM而不是HH:SS,但它很容易更改):
Time.now.strftime("%d/%m/%Y %H:%M")
#=> "14/09/2011 14:09"
针对换班进行了更新:
d = DateTime.now
d.strftime("%d/%m/%Y %H:%M")
#=> "11/06/2017 18:11"
d.next_month.strftime("%d/%m/%Y %H:%M")
#=> "11/07/2017 18:11"
您需要为此btw执行require 'date'
。
发布于 2011-09-14 23:03:44
require 'date'
current_time = DateTime.now
current_time.strftime "%d/%m/%Y %H:%M"
# => "14/09/2011 17:02"
current_time.next_month.strftime "%d/%m/%Y %H:%M"
# => "14/10/2011 17:02"
发布于 2011-09-14 20:24:31
time = Time.now.to_s
time = DateTime.parse(time).strftime("%d/%m/%Y %H:%M")
对于增量减少月份,请使用<< >>运算符
示例
datetime_month_before = DateTime.parse(time) << 1
datetime_month_before = DateTime.now << 1
https://stackoverflow.com/questions/7415982
复制相似问题