728x90
파이썬의 datetime 모듈을 이용하면 다양한 날짜와 시간 처리 작업을 손쉽게 수행할 수 있음
이를 통해 날짜와 시간을 생성하고 형식화하며, 연산하는 등의 작업을 효과적으로 처리할 수 있음
날짜, 시간 생성
이 모듈을 사용하여 현재 시간을 가져오거나 특정 날짜와 시간을 생성할 수 있음
import datetime
현재_시간 = datetime.datetime.now()
print("현재 시간:", 현재_시간)
특정_시간 = datetime.datetime(2024, 4, 12, 10, 30, 0) # 년, 월, 일, 시, 분, 초
print("특정 시간:", 특정_시간)
날짜 및 시간 형식화
import datetime
현재_시간 = datetime.datetime.now()
형식화된_시간 = 현재_시간.strftime('%Y-%m-%d %H:%M:%S')
print("형식화된 시간:", 형식화된_시간)
%Y, %m, %d, %H, %M, %S는 각각 연도, 월, 일, 시, 분, 초를 나타내는 포맷 문자열
timedelta를 이용한 날짜 및 시간 연산
import datetime
시간_차이 = datetime.timedelta(days=3, hours=5, minutes=30)
현재_시간 = datetime.datetime.now()
미래_시간 = 현재_시간 + 시간_차이
과거_시간 = 현재_시간 - 시간_차이
print("미래 시간:", 미래_시간)
print("과거 시간:", 과거_시간)
728x90
'Python' 카테고리의 다른 글
Python Singleton 패턴 (0) | 2024.04.12 |
---|---|
Python 날짜 시간 데이터 다루기 (feat. pandas) (1) | 2024.04.12 |
[작성중] Python 문서화 (feat. sphinx) (0) | 2024.04.12 |
Python 문서화 (feat. PEP 257) (0) | 2024.04.11 |
Python ZeroMQ (0) | 2024.04.11 |