datetime 모듈에서 datetime, timedelta 함수 import
문자열을 날짜로 변환
format | 설명 | 예시 |
%Y | 네자리 수 연도 | 2021, 2022 |
%y | 두자리 수 연도 | 21,22 |
%m | 0이 채워진 월 | 01,02,...,11,12 |
%d | 0이 채워진 일 | 01,02,...,30,31 |
%Y-%m-%d | 년-월-일 | 2022-06-02 |
시분초 제외 날짜만 표현
날짜를 문자열로 변환
datetime에서 날짜 계산
print(a := str_to_date-str_to_date_minus, type(a))
# 1 day, 0:00:00 <class 'datetime.timedelta'>
print(a.days)
# 1
딕셔너리에서 날짜별 온도 찾기
from datetime import datetime, timedelta
temperature = {'2022-05-31':25,'2022-06-01':27,'2022-06-02':30}
# 문자열을 날짜로 변환 datetime.strptime(string,format)
# 시분초 제외 날짜만 표현: datetime.date()
# print(datetime.strptime(date,'%Y-%m-%d').date())
# 날짜를 문자열로 변환 datetime.strftime(datetime,format)
today_date_string = input('오늘날짜 = ')
str_to_date = datetime.strptime(today_date_string,'%Y-%m-%d').date()
date_to_str = datetime.strftime(str_to_date,'%Y-%m-%d')
# print(str_to_date, type(str_to_date))
# print(date_to_str, type(date_to_str))
str_to_date_minus = str_to_date - timedelta(days=1)
str_to_date_plus = str_to_date + timedelta(days=1)
print(f'어제온도: {temperature[datetime.strftime(str_to_date_minus,"%Y-%m-%d")]}')
print(f'오늘온도: {temperature[today_date_string]}')
print(f'어제온도: {temperature[datetime.strftime(str_to_date_plus,"%Y-%m-%d")]}')
[python] Chrome Webdriver 업데이트 없이 selenium(셀레니움) 실행하는 방법 (1) | 2024.04.28 |
---|---|
[python] OCR 기능 활용, 이미지에서 한글 텍스트 읽어오기 (with 네이버 클로바 OCR) (0) | 2024.04.13 |
[Python] 내포 활용 예시(딕셔너리, 리스트, 정렬) (0) | 2022.08.07 |
[Python] 리스트 평탄화 - 내포 활용 (0) | 2022.08.07 |
[Python] 리스트 내포 if / else 구문 사용 방법 (0) | 2022.08.06 |
댓글 영역