Python을 사용하여 현재 날짜 가져오기
전제 조건: 날짜시간 모듈
Python에서 날짜와 시간은 자체 데이터 유형이 아니지만 날짜와 시간을 처리하기 위해 DateTime이라는 모듈을 가져올 수 있습니다. Datetime 모듈은 Python에 내장되어 있으므로 외부에 설치할 필요가 없습니다. 그만큼 날짜시간 모듈 현재 날짜와 시간을 가져오는 몇 가지 기능을 제공합니다.
date.today()를 사용하여 현재 날짜를 가져옵니다.
DateTime 모듈 아래 날짜 클래스의 today() 메서드는 오늘 날짜 값을 포함하는 날짜 객체를 반환합니다.
통사론: 날짜.오늘()
보고: 현재 현지 날짜를 반환합니다.
예:
파이썬3
# Import date class from datetime module> from> datetime> import> date> # Returns the current local date> today> => date.today()> print> (> 'Today date is: '> , today)> |
산출:
Today date is: 2019-12-11
datetime.now()를 사용하여 현재 날짜를 가져옵니다.
Python 라이브러리는 주로 현재 시간을 가져오는 데 사용할 수 있는 함수를 정의합니다. 날짜시간.지금() 함수를 실행하고 DateTime 모듈 아래에 정의된 현재 현지 날짜와 시간을 반환합니다.
통사론: datetime.now(tz)
매개변수: tz : 현재 시간과 날짜가 필요한 특정 시간대입니다. (기본적으로 그리니치 자오선 시간을 사용합니다.) 반품 : 현재 날짜와 시간을 시간 형식으로 반환합니다.
예:
파이썬3
# Import datetime class from datetime module> from> datetime> import> datetime> # returns current date and time> now> => datetime.now()> print> (> 'now = '> , now)> |
산출:
now = 2019-12-11 10:58:37.039404
지금()의 속성
now()에는 연도, 월, 날짜, 시, 분, 초와 같은 시간 속성과 동일한 다양한 속성이 있습니다.
예
파이썬3
# importing datetime module for now()> import> datetime> > # using now() to get current time> current_time> => datetime.datetime.now()> > # Printing attributes of now().> print> (> 'The attributes of now() are : '> )> > print> (> 'Year: '> , end> => '')> print> (current_time.year)> > print> (> 'Month: '> , end> => '')> print> (current_time.month)> > print> (> 'Day: '> , end> => '')> print> (current_time.day)> |
산출:
The attributes of now() are: Year: 2019 Month: 12 Day: 11
추천 기사 – Python을 사용하여 현재 날짜 및 시간 가져오기