Programming

현재 날짜 / 시간을 DD / MM / YYYY HH : MM 형식으로 가져 오려면 어떻게해야합니까?

procodes 2020. 8. 9. 10:32
반응형

현재 날짜 / 시간을 DD / MM / YYYY HH : MM 형식으로 가져 오려면 어떻게해야합니까?


현재 날짜와 시간을 DD/MM/YYYY HH:MM형식으로 가져오고 월을 늘리려면 어떻게해야합니까?


형식 지정은 다음과 같이 수행 할 수 있습니다 (HH : SS 대신 HH : MM을 의미한다고 가정했지만 변경하기 쉽습니다).

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"

require 'date'이 btw에 필요합니다 .


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"

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

날짜 :

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

위는 예를 들어 10/01/15로 표시됩니다.

그리고 시간 동안

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

위는 예를 들어 11:33으로 표시됩니다.

그런 다음 합치려면 끝에 추가하십시오.

puts date + " " + time

참고 URL : https://stackoverflow.com/questions/7415982/how-do-i-get-the-current-date-time-in-dd-mm-yyyy-hhmm-format

반응형