본문 바로가기
Python/Python_basic

날짜 포매팅 예제(from datetime import datetime)

by Mr.DonyStark 2023. 11. 13.

□ 현업에서 날짜 및 시간 포매팅은 중요한 요소중 하나임. 왜냐하면 log 관리기능을 위해 사용되기 때문임.
날짜계산, 수행시간계산, 로그 출력 포멧 규정등 프로그래밍에서 많이 활용됨

□ 참고 사이트

https://docs.python.org/ko/3/library/datetime.html#strftime-and-strptime-behavior

 

datetime — Basic date and time types

Source code: Lib/datetime.py The datetime module supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus of the implementation is on efficient attr...

docs.python.org

from datetime import datetime #관련 라이브러리 호출

#타임존 출력
now_time = datetime.now()
#'23-11-13' 형식 출력
print(datetime.now().strftime('%y-%m-%d'))
#'2023-11-13' 형식 출력
print(datetime.now().strftime('%Y-%m-%d'))
#'2023-11-13 17:18:36' 형식 출력
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
#'2023-11-13 17:18:36 PM' 형식 출력
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S %p'))
#'2023-11-13 17:20:50 PM Monday November' 형식 출력
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S %p %A %B'))
#'2023-11-13 17:21:10 PM Mon Nov' 형식 출력
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S %p %a %b'))
#'Monday,November,13,2023 17:22:36' 형식 출력
print(datetime.now().strftime('%A,%B,%d,%Y %H:%M:%S'))
#'Monday,Nov,11/13/23,05:23:40 PM' 형식 출력
print(datetime.now().strftime('%A,%b,%x,%r'))

'Python > Python_basic' 카테고리의 다른 글

조건에 의한 비밀번호 생성예제  (0) 2023.11.15
무작위 6자리 코드 생성 예제  (1) 2023.11.15
딕셔너리 조회 예제 (dictionary comprehension + input함수)  (1) 2023.11.13
타임 딜레이  (2) 2023.11.08
enumerate()  (0) 2023.11.08