2020/Python
Python timedelta 두 datetime의 차이값
꽃꽂이하는개발자
2019. 11. 20. 19:20
반응형
현재 날짜 시간과 과거의 날짜시간의 대소를 비교해볼까요?
current = datetime.datetime.now() #현재 날짜 시간
past = datetime.datetime(2001, 1,1) #과거 날짜 시간
print(current >past) #current가 past보다 매리인가요?
출력해보면 True값이 뜨게 됩니다
# 두 datetime은 차이값을 구할 수 있을까요?
diff = current - past
print(diff)
이렇게 출력이 되는군요^^
#timedelta의 total_second -> 모든 속성을 합산 초단위로 반환
print(diff.days, diff.seconds, diff.microsecond, diff.total_second())
# 현재로부터 365일이 지난 시점의 datetime을 알아봅시다
print("current:", current) # 현재의 시점이 나오죠
future = current + datetime.timedelta(day=365, seconds = 0, microseconds= 0)
print("future:", future)
네 이렇게 출력이 되네요^^
반응형