시작하기무료로 시작하기

날짜 형식 변환 - 기초

유럽의 판매 지역 A와 호주의 판매 지역 B는 서로 다른 날짜 형식을 사용해요.

  • Sale A: 2018년 02월 14일에 4000
  • Sale B: 2018년 3월 2일에 3000

판매 기간을 통합하거나 비교하려면, 날짜를 동일한 형식으로 변환해야 해요. datetime 라이브러리와 datetime.strptime(date_string, format) 메서드를 사용하면 쉽게 처리할 수 있어요. 다음 지시자를 참고하세요:

Directive Meaning Example
%d Day of the month as a zero-padded decimal number 01, 02, …, 31
%b Month as locale’s abbreviated name Jan, Feb, …, Dec
%B Month as locale’s full name January, …, December
%m Month as a zero-padded decimal number 01, 02, …, 12
%y Year without century as a zero-padded decimal number 00, 01, …, 99
%Y Year with century as a decimal number 1970, 1988, 2001, 2013

이 연습은 강의의 일부입니다

Python으로 하는 Financial Forecasting

강의 보기

연습 안내

  • datetime 라이브러리를 임포트하세요.
  • 각 판매 내역의 날짜를 일-월-연도 형식으로 표준화해 변환하는 dt_object를 생성하세요.
  • 각 결과를 출력해 비교하세요

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Import the datetime python library
from ____ import ____

# Create a dt_object to convert the first date and print the month result
dt_object1 = datetime.strptime('14/02/2018', '____')
print(____)

# Create a dt_object to convert the second date and print the month result
dt_object2 = datetime.strptime('2 March 2018', '____')
print(____)
코드 편집 및 실행