python datetime 사용법 정리

python datetime 사용법 정리

Pandas
python datetime 사용법 정리
Author

gabriel yang

Published

November 29, 2023

python datetime 사용법 정리

Python의 datetime 모듈은 날짜와 시간을 다루기 위한 다양한 함수와 클래스를 제공합니다. 시간 데이터를 관리하기 위한 datetime정보를 정리합니다.

현재 시간 확인하기

import datetime as dt
now = dt.datetime.now()
now
datetime.datetime(2024, 6, 24, 22, 41, 32, 250632)

datetime의 현재 시간정보를 보여준다. 시간 정보는 datetime 형태로 표현됩니다. 날짜에서 원하는 시간정보를 가져올 수 있습니다.

now = dt.datetime.now()
print(f"{now}")
print(f"{now.year}")
print(f"{now.month}")
print(f"{now.day}")
print(f"{now.hour}")
print(f"{now.minute}")
print(f"{now.second}")
print(f"{now.microsecond}")
2024-06-24 22:41:32.258487
2024
6
24
22
41
32
258487

원하는 형태의 문자로 변경

시간을 원하는 형태의 문자열 형태로 표현하는 경우가 많습니다. strftime 메서드를 이용해서 날짜를 표현할 표현형식을 변경합니다.

print(now.strftime("%Y-%m-%d %H:%M:%S"))
2024-06-24 22:41:32

시간 변경하기

정해진 기간의 시간을 더하거나 빼는 경우 timedelta를 사용합니다.

print(f"now: {now}")
print(f"{now + dt.timedelta(days=1)} + 1 day")
print(f"{now + dt.timedelta(hours=1)} + 1 hour")
print(f"{now + dt.timedelta(minutes=1)} + 1 min")
print(f"{now + dt.timedelta(seconds=1)} + 1 sec")
print(f"{now + dt.timedelta(microseconds=1)} + 1 micro sec")
now: 2024-06-24 22:41:32.258487
2024-06-25 22:41:32.258487 + 1 day
2024-06-24 23:41:32.258487 + 1 hour
2024-06-24 22:42:32.258487 + 1 min
2024-06-24 22:41:33.258487 + 1 sec
2024-06-24 22:41:32.258488 + 1 micro sec

References

  1. https://blog.qvil.dev/docker/docker-mongo