AI/Python

[Python] datetime 모듈

i-moo 2025. 9. 25. 09:22
반응형
import datetime

현재 시간 출력하기

now = datetime.datetime.now()
#datetime.datetime(2025, 9, 25, 8, 59, 22, 622938)

시간 포맷 맞춰서 출력하기

now.strftime("%Y.%m.%d %H:%M:%S")
# 25.09.25 08:59:22

now.strftime("%Y.%m.%d %H:%M:%S")
# 2025.09.25 08:59:22

now.strftime("%D")
# 09/25/25

now.strftime("%y{}%m{}%d{} %H:%M:%S").format(*"년월일")
#25년09월25일 08:59:22

시간 더하기

 

datetime — Basic date and time types

Source code: Lib/datetime.py The datetime module supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus of the implementation is on efficient attr...

docs.python.org

now + datetime.timedelta(days=1)
# datetime.datetime(2025, 9, 26, 8, 59, 22, 622938)

특정 시간으로 바꾸기

now.replace(year=2050)
# datetime.datetime(2050, 9, 25, 8, 59, 22, 622938)
반응형