[python] pytz를 사용하여 특정 시간대에서 현재 날짜를 가져오는 방법은 무엇인가요?
from datetime import datetime
import pytz

# 특정 시간대 생성
timezone = pytz.timezone('Asia/Seoul')

# 현재 날짜 및 시간 가져오기
current_datetime = datetime.now(timezone)
current_date = current_datetime.date()

print(current_date)

위의 예시 코드는 pytz 모듈을 사용하여 ‘Asia/Seoul’ 시간대에서 현재 날짜를 가져오는 방법을 보여줍니다. datetime.now() 함수를 사용하여 현재 날짜와 시간을 가져온 후, date() 메서드를 호출하여 날짜 부분만 추출합니다.

결과:

2022-01-01

위의 결과는 현재 시스템 시간으로부터 ‘Asia/Seoul’ 시간대에 해당하는 날짜를 나타냅니다.

이 예시 코드를 실행하면 다른 시간대에서도 동일한 방법으로 현재 날짜를 가져올 수 있습니다. pytz.timezone() 함수를 사용하여 원하는 시간대를 설정하면 됩니다.

참고 문서: