How can I get the current date and time in DD/MM/YYYY HH:MM
format and also increment the month?
如何以DD/MM/ yyyyyyyyyhh:MM格式获取当前日期和时间,并增加月份?
4 个解决方案
#1
143
The formatting can be done like this (I assumed you meant HH:MM instead of HH:SS, but it's easy to change):
格式可以这样做(我以为你指的是HH:MM而不是HH:SS,但是很容易修改):
Time.now.strftime("%d/%m/%Y %H:%M")
#=> "14/09/2011 14:09"
Updated for the shifting:
更新后的变化:
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"
You need to require 'date'
for this btw.
顺便说一句,你需要为此要求“日期”。
#2
19
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"
#3
8
time = Time.now.to_s
time = DateTime.parse(time).strftime("%d/%m/%Y %H:%M")
for increment decrement month use << >> operators
对于递增递减月,使用< >>操作符
examples
例子
datetime_month_before = DateTime.parse(time) << 1
datetime_month_before = DateTime.now << 1
#4
4
For date:
日期:
#!/usr/bin/ruby -w
date = Time.new
#set 'date' equal to the current date/time.
date = date.day.to_s + "/" + date.month.to_s + "/" + date.year.to_s
#Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display DD/MM/YYYY
puts date
#output the date
The above will display, for example, 10/01/15
上面将显示,例如,10/01/15
And for time
和时间
time = Time.new
#set 'time' equal to the current time.
time = time.hour.to_s + ":" + time.min.to_s
#Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display hour and minute
puts time
#output the time
The above will display, for example, 11:33
上面将显示,例如,11:33
Then to put it together, add to the end:
然后把它们放在一起,加到最后:
puts date + " " + time
#1
143
The formatting can be done like this (I assumed you meant HH:MM instead of HH:SS, but it's easy to change):
格式可以这样做(我以为你指的是HH:MM而不是HH:SS,但是很容易修改):
Time.now.strftime("%d/%m/%Y %H:%M")
#=> "14/09/2011 14:09"
Updated for the shifting:
更新后的变化:
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"
You need to require 'date'
for this btw.
顺便说一句,你需要为此要求“日期”。
#2
19
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"
#3
8
time = Time.now.to_s
time = DateTime.parse(time).strftime("%d/%m/%Y %H:%M")
for increment decrement month use << >> operators
对于递增递减月,使用< >>操作符
examples
例子
datetime_month_before = DateTime.parse(time) << 1
datetime_month_before = DateTime.now << 1
#4
4
For date:
日期:
#!/usr/bin/ruby -w
date = Time.new
#set 'date' equal to the current date/time.
date = date.day.to_s + "/" + date.month.to_s + "/" + date.year.to_s
#Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display DD/MM/YYYY
puts date
#output the date
The above will display, for example, 10/01/15
上面将显示,例如,10/01/15
And for time
和时间
time = Time.new
#set 'time' equal to the current time.
time = time.hour.to_s + ":" + time.min.to_s
#Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display hour and minute
puts time
#output the time
The above will display, for example, 11:33
上面将显示,例如,11:33
Then to put it together, add to the end:
然后把它们放在一起,加到最后:
puts date + " " + time