날짜를 문자열로 변환하기

Python에서 날짜와 시간 다루기

Max Shron

Data Scientist and Author

ISO 8601 형식

from datetime import date

# 예시 날짜
d = date(2017, 11, 5)

# ISO 형식: YYYY-MM-DD print(d)
2017-11-05
# 날짜를 ISO 8601 형식으로 표현해 리스트에 넣기
print( [d.isoformat()] )
['2017-11-05']
Python에서 날짜와 시간 다루기

ISO 8601 형식

# 과거에 컴퓨터가 처리에 어려움을 겪던 날짜들
some_dates = ['2000-01-01', '1999-12-31']

# 정렬하여 출력 print(sorted(some_dates))
['1999-12-31', '2000-01-01']
Python에서 날짜와 시간 다루기

기타 모든 형식

d.strftime()
Python에서 날짜와 시간 다루기

기타 형식: strftime

# 예시 날짜
d = date(2017, 1, 5)

print(d.strftime("%Y"))
2017
# 더 많은 텍스트가 포함된 형식 문자열
print(d.strftime("Year is %Y"))
Year is 2017
Python에서 날짜와 시간 다루기

기타 형식: strftime

# 형식: YYYY/MM/DD
print(d.strftime("%Y/%m/%d"))
2017/01/05
Python에서 날짜와 시간 다루기

날짜를 문자열로 변환하기

Python에서 날짜와 시간 다루기

Preparing Video For Download...