How to get the current date / time in the format DD / MM / YYYY HH: MM?

How to get the current date and time in DD/MM/YYYY HH:MM format, as well as increase the month?

+83
ruby
Sep 14 '11 at 12:01
source share
4 answers

Formatting can be done as follows (I assumed that you meant HH: MM instead of HH: SS, but it is easy to change it):

 Time.now.strftime("%d/%m/%Y %H:%M") #=> "14/09/2011 14:09" 

Updated to switch:

 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" 

For this btw you need require 'date' .

+133
Sep 14 '11 at 12:05
source share
 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" 
+17
Sep 14 '11 at 15:03
source share
 time = Time.now.to_s time = DateTime.parse(time).strftime("%d/%m/%Y %H:%M") 
<<<<<<β†’ Operators

examples

 datetime_month_before = DateTime.parse(time) << 1 datetime_month_before = DateTime.now << 1 
+8
Sep 14 '11 at 12:24
source share

Date of:

 #!/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 

In the above example, for example, 10/01/15 will be displayed

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

Then, to compose it, add at the end:

 puts date + " " + time 
+3
Jan 10 '15 at 11:33
source share



All Articles