Created
February 6, 2024 07:57
-
-
Save 6mini/549da0c1ae8c9f821fe05f5d168fdb07 to your computer and use it in GitHub Desktop.
UTC 시간을 기준으로 한국 시간대(KST)의 '오늘'과 '어제' 날짜를 계산하고, 이를 문자열 형식으로 반환한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, timedelta | |
def get_kst_dates(): | |
""" | |
현재 UTC 시간을 기준으로 한국 시간대(KST)의 '오늘'과 '어제' 날짜를 문자열 형태로 반환한다. | |
반환값: | |
today_str (str): 오늘 날짜 (KST)의 문자열 형태 ('YYYY-MM-DD'). | |
yesterday_str (str): 어제 날짜 (KST)의 문자열 형태 ('YYYY-MM-DD'). | |
""" | |
# 현재 UTC 시간 | |
now_utc = datetime.utcnow() | |
# 한국 시간 | |
kst_offset = timedelta(hours=9) | |
now_kst = now_utc + kst_offset | |
# 오늘 날짜 (KST) | |
today = now_kst | |
# 어제 날짜 (KST) | |
yesterday = today - timedelta(days=1) | |
# 날짜 형식 변경 | |
today_str = today.strftime('%Y-%m-%d') | |
yesterday_str = yesterday.strftime('%Y-%m-%d') | |
return today_str, yesterday_str | |
# 함수 사용 예제 | |
today_str, yesterday_str = get_kst_dates() | |
print("오늘 날짜 (KST):", today_str) | |
print("어제 날짜 (KST):", yesterday_str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment