[날짜 다루기]
from math import exp, log, sqrt
import re
from datetime import date, time, datetime, timedelta
today = date.today()
print("Output #41: today:{0!s}".format(today))
print("Output #42: year:{0!s}".format(today.year))
print("Output #43: month:{0!s}".format(today.month))
print("Output #44: day:{0!s}".format(today.day))
current_datetime = datetime.today()
print("Output #45: datetime:{0!s}".format(current_datetime))
Output #41: today:2018-02-07 Output #42: year:2018 Output #43: month:2 Output #44: day:7 Output #45: day:2018-02-07 08:16:54.543714
#timedelta 함수로 새로운 날짜 만들기
one_day = timedelta(days=-1)
yesterday = today + one_day
print("Output #46: yesterday:{0!s}".format(yesterday))
eight_hours = timedelta(hours=-8)
print("Output #47: eight hours:{0!s} {1!s}".format(eight_hours.days, eight_hours.seconds))
Output #46: yesterday:2018-02-06 Output #47: eight hours:-1 57600
#두 날짜 사이의 차이 계산
date_diff = today - yesterday
print("Output #48: diff:{0!s}".format(date_diff))
print("Output #49: diff:{0!s}".format(str(date_diff).split()[0]))
Output #48: diff:1 day, 0:00:00 Output #49: diff:1
# strftime함수로 date 객체를 특적 문자열로 만들기
print("Output #50: {0!s}".format(today.strftime('%m/%d/%Y')))
print("Output #51: {0!s}".format(today.strftime('%b %d, %Y')))
print("Output #52: {0!s}".format(today.strftime('%Y-%m-%d')))
print("Output #53: {0!s}".format(today.strftime('%B %d, %Y')))
Output #50: 02/07/2018 Output #51: Feb 07, 2018 Output #52: 2018-02-07 Output #53: February 07, 2018
date1 = today.strftime('%m/%d/%Y')
date2 = today.strftime('%b %d, %Y')
date3 = today.strftime('%Y-%m-%d')
date4 = today.strftime('%B %d, %Y')
print("Output #54: {0!s}".format(datetime.strptime(date1, '%m/%d/%Y')))
print("Output #55: {0!s}".format(datetime.strptime(date2, '%b %d, %Y')))
print("Output #56: {0!s}".format(datetime.date(datetime.strptime(date3, '%Y-%m-%d'))))
print("Output #57: {0!s}".format(datetime.date(datetime.strptime(date4, '%B %d, %Y'))))
Output #54: 2018-02-07 00:00:00 Output #55: 2018-02-07 00:00:00 Output #56: 2018-02-07 Output #57: 2018-02-07
'Data > Python' 카테고리의 다른 글
Web Scraper 기본 (0) | 2018.05.18 |
---|---|
Python IDE 개발환경, pip proxy ssl 문제해결 (0) | 2018.05.10 |
자료형 - List (0) | 2018.02.13 |
Hello python, ipynb 파일 py 파일로 만들기, 실행, 자료형 (2) | 2018.01.09 |
NLTK 설치 및 수동 다운로드, 토큰화 테스트 (1) | 2018.01.04 |