Date 클래스
Date 클래스
Date 클래스는 달력 날짜를 저장하고 다루는 메서드를 제공해요.
다음 경우라면 Date 대신 Time 클래스를 고려해 보세요:
- 날짜와 시간이 둘 다 필요할 때 (Date는 날짜만 다뤄요).
- 그레고리력 날짜만 필요할 때 (율리우스력은 다루지 않아요).
한 번 만들어진 Date 객체는 불변이에요. 수정할 수 없죠.
Date 만들기
Date.today로 오늘 날짜를 만들 수 있어요:
Date.today # => #<Date: 1999-12-31>
인자 조합이 다양한 특정 날짜를 만들 수도 있어요:
-
Date.new— 정수 연, 월, 일:Date.new(1999, 12, 31) # => #<Date: 1999-12-31> -
Date.ordinal— 정수 연과 연중 일:Date.ordinal(1999, 365) # => #<Date: 1999-12-31> -
Date.jd— 정수 율리우스 일:Date.jd(2451544) # => #<Date: 1999-12-31> -
Date.commercial— 정수 상용 데이터(연, 주, 요일):Date.commercial(1999, 52, 5) # => #<Date: 1999-12-31> -
Date.parse— 문자열을 휴리스틱하게 파싱:Date.parse('1999-12-31') # => #<Date: 1999-12-31> Date.parse('31-12-1999') # => #<Date: 1999-12-31> Date.parse('1999-365') # => #<Date: 1999-12-31> Date.parse('1999-W52-5') # => #<Date: 1999-12-31> -
Date.strptime— 날짜 문자열과 형식 문자열을 받아 그 형식대로 파싱:Date.strptime('1999-12-31', '%Y-%m-%d') # => #<Date: 1999-12-31> Date.strptime('31-12-1999', '%d-%m-%Y') # => #<Date: 1999-12-31> Date.strptime('1999-365', '%Y-%j') # => #<Date: 1999-12-31> Date.strptime('1999-W52-5', '%G-W%V-%u') # => #<Date: 1999-12-31>
인자 limit
문자열 인자를 파싱하는 Date의 일부 싱글턴 메서드는 선택 키워드 인자 limit을 받아서, 문자열 인자의 길이를 제한할 수 있어요.
limit이:
- 음이 아닌 수: 문자열 길이가 limit보다 크면
ArgumentError. - 다른 숫자나
nil:limit무시. - 다른 비-숫자:
TypeError.
상수
ABBR_DAYNAMES—English의 요일 약어 문자열 배열. 첫 번째는 "Sun".ABBR_MONTHNAMES—English의 월 약어 문자열 배열. 첫 요소는 nil.DAYNAMES— 요일 전체 이름 문자열 배열. 첫 번째는 "Sunday".ENGLAND— 영국과 그 식민지의 달력 개혁일의 율리우스 일수.GREGORIAN— proleptic 그레고리력의 달력 개혁일 율리우스 일수.ITALY— 이탈리아와 일부 가톨릭 국가의 달력 개혁일 율리우스 일수.JULIAN— proleptic 율리우스력의 달력 개혁일 율리우스 일수.MONTHNAMES—English의 월 전체 이름 문자열 배열. 첫 요소는 nil.
클래스 메서드
-
_httpdate(string, limit: 128) → hash— 유효한 HTTP 날짜 형식이어야 하는string에서 파싱한 값들의 해시를 돌려줘요.d = Date.new(2001, 2, 3) s = d.httpdate # => "Sat, 03 Feb 2001 00:00:00 GMT" Date._httpdate(s) # => {:wday=>6, :mday=>3, :mon=>2, :year=>2001, :hour=>0, :min=>0, :sec=>0, :zone=>"GMT", :offset=>0}관련:
Date.httpdate(Date 객체를 돌려줌). -
_iso8601(string, limit: 128) → hash— ISO 8601 형식의 날짜를 담은string에서 파싱한 값들의 해시:Date._iso8601(s) # => {:mday=>3, :year=>2001, :mon=>2}. 관련:Date.iso8601. -
_jisx0301(string, limit: 128) → hash— 유효한 JIS X 0301 날짜 형식인string에서 파싱한 값들의 해시:Date._jisx0301(s) # => {:year=>2001, :mon=>2, :mday=>3}. 관련:Date.jisx0301. -
_parse(string, comp = true, limit: 128) → hash—string에서 파싱한 값들의 해시.comp가true이고 주어진 연도가(0..99)범위면 현재 세기를 더해 줘요, 아니면 그대로:Date._parse('2001-02-03') # => {:year=>2001, :mon=>2, :mday=>3} Date._parse('01-02-03', true) # => {:year=>2001, :mon=>2, :mday=>3} Date._parse('01-02-03', false) # => {:year=>1, :mon=>2, :mday=>3}이 메서드는 validator가 아니에요. 유효하지 않은 날짜를 주면 결과가 예측 불가해요.
Date._strptime을 쓰는 걸 고려하세요. 관련:Date.parse. -
_rfc2822(string, limit: 128) → hash— RFC 2822 형식인string에서 파싱한 값들의 해시. 관련:Date.rfc2822. -
_rfc3339(string, limit: 128) → hash— RFC 3339 형식인string에서 파싱한 값들의 해시:Date._rfc3339(s) # => {:year=>2001, :mon=>2, :mday=>3, ...}. 관련:Date.rfc3339. -
_strptime(string, format = '%F') → hash— 주어진format에 따라string에서 파싱한 값들의 해시. (Date.strftime과 달리 플래그와 폭을 지원하지 않아요.)Date._strptime('2001-02-03', '%Y-%m-%d') # => {:year=>2001, :mon=>2, :mday=>3}. 관련:Date.strptime. -
_xmlschema(string, limit: 128) → hash— 유효한 XML 날짜 형식인string에서 파싱한 값들의 해시:Date._xmlschema(s) # => {:year=>2001, :mon=>2, :mday=>3}. 관련:Date.xmlschema. -
commercial(cwyear = -4712, cweek = 1, cwday = 1, start = Date::ITALY) → date— 인자로 만든 새Date객체.cwyear는 연,cweek는 연중 주 인덱스(1..53 또는 -53..-1),cwday는 주중 요일 인덱스(1..7 또는 -7..-1).cweek가 1일 때 1월 1일이 금·토·일이면 첫 주는 그 다음 주에 시작해요.Date.commercial(2022, 1, 1).to_s # => "2022-01-03" Date.commercial(2022, 52, 1).to_s # => "2022-12-26"관련:
Date.jd,Date.new,Date.ordinal. -
gregorian_leap?(year) → true or false— 주어진 연도가 proleptic 그레고리력에서 윤년이면true.Date.gregorian_leap?(2000) # => true,Date.gregorian_leap?(2001) # => false. 관련:Date.julian_leap?. -
httpdate(string = 'Mon, 01 Jan -4712 00:00:00 GMT', start = Date::ITALY, limit: 128) → date— 유효한 HTTP 날짜 형식인string에서 파싱한 값으로 새Date객체.Date.httpdate(s) # => #<Date: 2001-02-03>. 관련:Date._httpdate. -
iso8601(string = '-4712-01-01', start = Date::ITALY, limit: 128) → date— ISO 8601 형식 날짜를 담은string에서 파싱한 새Date객체.Date.iso8601(s) # => #<Date: 2001-02-03>. 관련:Date._iso8601. -
jd(jd = 0, start = Date::ITALY) → date— 인자로 만든 새Date객체:Date.jd(2451944).to_s # => "2001-02-03" Date.jd(0).to_s # => "-4712-01-01"돌려받은 날짜는 인자가
start이상이면 그레고리력, 아니면 율리우스력. 관련:Date.new,Date.ordinal,Date.commercial. -
julian_leap?(year) → true or false— proleptic 율리우스력에서 윤년이면true. 관련:Date.gregorian_leap?. -
new(year = -4712, month = 1, mday = 1, start = Date::ITALY) → date— 인자로 만든 새Date객체:Date.new(2001, 2, 3).to_s # => "2001-02-03"관련:
Date.jd,Date.ordinal,Date.commercial. -
ordinal(year = -4712, yday = 1, start = Date::ITALY) → date— 정수 연과 연중 일로 만든 새Date객체:Date.ordinal(2001, 34).to_s # => "2001-02-03". 관련:Date.jd,Date.new,Date.commercial. -
parse(string = '-4712-01-01', comp = true, start = Date::ITALY, limit: 128) → date—string에서 파싱한 값으로 새Date객체.comp가 true면 "00"~"99" 범위 연도를 full로 만들어요. validator가 아니라는 점 유의. 관련:Date._parse. -
rfc2822(string = 'Mon, 1 Jan -4712 00:00:00 +0000', start = Date::ITALY, limit: 128) → date— RFC 2822 형식인string에서 파싱한 새Date객체. 관련:Date._rfc2822. -
rfc3339(string = '-4712-01-01T00:00:00+00:00', start = Date::ITALY, limit: 128) → date— RFC 3339 형식인string에서 파싱한 새Date객체. 관련:Date._rfc3339. -
strptime(string = '-4712-01-01', format = '%F', start = Date::ITALY) → date—format에 따라string을 파싱해 만든 새Date객체:Date.strptime('2001-02-03', '%Y-%m-%d') # => #<Date: 2001-02-03>. 관련:Date._strptime. -
today(start = Date::ITALY) → date— 오늘 날짜를 나타내는 새Date객체:Date.today # => #<Date: 1999-12-31> -
valid_civil?(year, month, mday, start = Date::ITALY) → true or false— 주어진 인자가 유효한 민력(그레고리/율리우스) 날짜면true.Date.valid_date?의 별칭. 관련:Date.valid_jd?,Date.valid_ordinal?,Date.valid_commercial?. -
valid_commercial?(cwyear, cweek, cwday, start = Date::ITALY) → true or false— 유효한 상용(주 기반) 날짜면true. -
valid_jd?(jd, start = Date::ITALY) → true— 유효한 율리우스 일이면true(항상 true). -
valid_ordinal?(year, yday, start = Date::ITALY) → true or false— 유효한 서수 날짜면true. -
xmlschema(string = '-4712-01-01', start = Date::ITALY, limit: 128) → date— 유효한 XML 날짜 형식인string에서 파싱한 새Date객체:Date.xmlschema(s) # => #<Date: 2001-02-03>. 관련:Date._xmlschema.
인스턴스 메서드
-
d + other → date—d에other(일수,Integer/Rational/Numeric)를 더한 새Date.d = Date.new(1999, 12, 31) d + 1 # => #<Date: 2000-01-01> -
d - other → date or rational—other가 일수면d에서 뺀 새Date;other가Date면 그 둘 사이의 일수(율리우스 일 차)의Rational. 관련:d - 365. -
d << n → date—d에서n개월 뒤로 간 새Date(월이 day로 변환돼요):Date.new(2001,2,3) << 1 #=> #<Date: 2001-01-03> Date.new(2001,2,3) << -2 #=> #<Date: 2001-04-03> -
self <=> other → -1, 0, 1 or nil— 비교 결과:other가 크면-1, 같으면0,other가 작으면1, 비교 불가면nil.d = Date.new(1999,12,31) d <=> Date.new(2000,1,1) #=> -1 d <=> Date.new(1999,12,31) #=> 0 d <=> Date.new(1999,12,30) #=> 1 -
self === other → true, false, or nil—other가 범위 안의 날짜면true. (Range#===와 동작이 다르다는 점 유의.) -
d >> n → new_date—d에서n개월 앞으로 간 새Date(월이 day로 변환):Date.new(2001,2,3) >> 1 #=> #<Date: 2001-03-03> Date.new(2001,2,3) >> -2 #=> #<Date: 2000-12-03> -
ajd → rational—self의 천문 율리우스 일수(Astronomical Julian Day Number):Date.new(2001,2,3).ajd #=> (1176937217/48) -
amjd → rational— 수정된 율리우스 일수(Astronomical Modified Julian Day Number):Date.new(2001,2,3).amjd #=> (4863209/2) -
asctime → string— RFC 2822 형식의 문자열:Date.new(2001,2,3).asctime #=> "Sat Feb 3 00:00:00 2001" -
cwday → integer— 주 기반(commercial) 요일(1-7, 월요일=1):Date.new(2001,2,3).cwday #=> 6 -
cweek → integer— 주 기반 주 인덱스(1-53):Date.new(2001,2,3).cweek #=> 5 -
cwyear → integer— 주 기반 연도:Date.new(2001,2,3).cwyear #=> 2001 -
day_fraction → rational— 그 날의 지난 시간 부분을 나타내는 분수:Date.new(2001,2,3).day_fraction #=> (0/1) -
deconstruct_keys(array_of_names_or_nil) → hash— 패턴 매칭용 이름/값 해시:Date.new(2001,2,3).deconstruct_keys([:year, :mon]) #=> {:year=>2001, :mon=>2} -
downto(min){|date| ... } → self—min까지 하루씩 내려가며 블록 호출. 블록이 없으면Enumerator. 관련:upto,step.d = Date.new(2001,2,3) a = [] d.downto(Date.new(2001,1,31)) {|date| a << date} # => #<Date: 2001-02-03> a # => [2001-02-03, 2001-02-02, 2001-02-01, 2001-01-31] -
england → new_date— 달력 개혁 기준이Date::ENGLAND인 새Date. -
friday? → true or false— 금요일이면true. -
gregorian → new_date— 그레고리력으로 표현한 새Date:Date.new(2001,2,3).gregorian #=> #<Date: 2001-02-03> -
gregorian? → true or false— 날짜가 그레고리력으로 표현되면true:Date.new(2001,2,3).gregorian? #=> true -
httpdate → string— HTTP 형식 문자열:Date.new(2001,2,3).httpdate #=> "Sat, 03 Feb 2001 00:00:00 GMT" -
infinite? → false— 유한한 날짜이므로 항상false. -
inspect → string— 사람이 읽을 수 있는 문자열:Date.new(2001,2,3).inspect #=> "#<Date: 2001-02-03>" -
iso8601 → string— ISO 8601 형식(Y-M-D):Date.new(2001,2,3).iso8601 #=> "2001-02-03" -
italy → new_date— 달력 개혁 기준이Date::ITALY인 새Date. -
jd → integer— 율리우스 일수:Date.new(2001,2,3).jd #=> 2451944 -
jisx0301 → string— JIS X 0301 형식:Date.new(2001,2,3).jisx0301 #=> "H13.02.03" -
julian → new_date— 율리우스력으로 표현한 새Date. -
julian? → true or false— 율리우스력으로 표현되면true:Date.new(2001,2,3).julian? #=> false -
ld → integer— 리리안 일수(Lilian day number):Date.new(2001,2,3).ld #=> 152790 -
leap? → true or false— 윤년이면true:Date.new(2000,1,1).leap? #=> true,Date.new(2001,1,1).leap? #=> false -
mday → integer— 달 안의 일(1-31):Date.new(2001,2,3).mday #=> 3 -
mjd → integer— 수정된 율리우스 일수:Date.new(2001,2,3).mjd #=> 51910 -
mon → integer— 월(1-12):Date.new(2001,2,3).mon #=> 2 -
monday? → true or false— 월요일이면true. -
new_start(start = Date::ITALY) → new_date— 달력 개혁 기준을start로 바꾼 새Date. -
next → new_date— 하루 뒤.Date#succ의 별칭. -
next_day(n = 1) → new_date—n일 뒤:Date.new(2001,2,3).next_day(2) #=> #<Date: 2001-02-05> -
next_month(n = 1) → new_date—n개월 뒤:Date.new(2001,2,3).next_month(2) #=> #<Date: 2001-04-03> -
next_year(n = 1) → new_date—n년 뒤:Date.new(2001,2,3).next_year(2) #=> #<Date: 2003-02-03> -
prev_day(n = 1) → new_date—n일 전:Date.new(2001,2,3).prev_day(2) #=> #<Date: 2001-02-01> -
prev_month(n = 1) → new_date—n개월 전:Date.new(2001,2,3).prev_month(2) #=> #<Date: 2000-12-03> -
prev_year(n = 1) → new_date—n년 전:Date.new(2001,2,3).prev_year(2) #=> #<Date: 1999-02-03> -
rfc2822 → string(rfc822별칭) — RFC 2822 형식:Date.new(2001,2,3).rfc2822 #=> "Sat, 3 Feb 2001 00:00:00 +0000" -
rfc3339 → string— RFC 3339 형식:Date.new(2001,2,3).rfc3339 #=> "2001-02-03T00:00:00+00:00" -
saturday? → true or false— 토요일이면true. -
start → float— 달력 개혁 기준의 율리우스 일수:Date.new(2001,2,3).start #=> 2299161.0 -
step(limit, step = 1){|date| ... } → self—self에서limit까지step만큼 건너뛰며 블록 호출. 블록 없이Enumerator. 관련:upto,downto. -
strftime(format = '%F') → string—format에 따라 형식화된 문자열:Date.new(2001,2,3).strftime('%Y-%m-%d') #=> "2001-02-03" -
sunday? → true or false— 일요일이면true. -
thursday? → true or false— 목요일이면true. -
to_date → self—self를 그대로 돌려줘요. -
to_datetime → datetime—self의 자정을 나타내는DateTime:Date.new(2001,2,3).to_datetime #=> #<DateTime: 2001-02-03T00:00:00+00:00 ...> -
to_s → string— ISO 8601 형식 문자열:Date.new(2001,2,3).to_s #=> "2001-02-03" -
to_time → time—self를 나타내는Time(시스템 로케일에 따라):Date.new(2001,2,3).to_time #=> 2001-02-03 00:00:00 +0900 -
tuesday? → true or false— 화요일이면true. -
upto(max){|date| ... } → self—max까지 하루씩 올라가며 블록 호출. 블록 없이Enumerator. -
wday → integer— 요일 인덱스(0-6, 일요일=0):Date.new(2001,2,3).wday #=> 6 -
wednesday? → true or false— 수요일이면true. -
yday → integer— 연중 일(1-366):Date.new(2001,2,3).yday #=> 34 -
year → integer— 연도:Date.new(2001,2,3).year #=> 2001