programing

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

oldcodes 2023. 6. 3. 08:42
반응형

현재 날짜/시간을 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'이번에는.

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

반응형