datetime 모듈

  • datetime 클래스
    • weekday()
      0:월, 1:화, 2:수, 3:목, 4:금, 5:토, 6:일
    • >>> from datetime import datetime
      >>> cur = datetime.now()
      >>> cur
      datetime.datetime(2019, 5, 4, 17, 51, 56, 408364)
      >>> cur.year
      2019
      >>> cur.month
      5
      >>> cur.day
      4
      >>> cur.hour
      17
      >>> cur.minute
      51
      >>> cur.second
      56
      >>> cur.microsecond
      408364
      >>> cur.weekday()  
      5
      >>> cur.strftime("%Y/%m/%d %H:%M:%S %A %B")
      '2019/05/04 17:51:56 Saturday May'
      >>> cur.date()
      datetime.date(2019, 5, 4)
      >>> cur.time()
      datetime.time(17, 51, 56, 408364)
  • timedelta 클래스
    날짜/시간 연산
    • total_seconds()
      모든 속성을 초.마이크로초로 반환
    • >>> start = datetime.now()
      >>> end = datetime.now()
      >>> elpsed = end - start
      >>> elpsed
      datetime.timedelta(0, 9, 121426)
      >>> elpsed.days, elpsed.seconds, elpsed.microseconds
      (0, 9, 121426)
      >>> elpsed.total_seconds()
      9.121426
      >>> from datetime import timedelta
      >>> datetime(2019, 5, 4) + timedelta(days=10)
      datetime.datetime(2019, 5, 14, 0, 0)

dateutil

  • 설치 pip install python-dateutil
  • function parse
    자동으로 형식 문자열을 찾아 datetime.datetime 클래스 객체를 만들어 준다.
    • 월, 일
      • 월과 일이 모두 12보다 작으면 먼저 나오는 숫자를 월로 판단한다.
      • 12보다 큰 숫자를 일로 판단한다.
    • >>> from dateutil.parser import parse
      >>> parse('2016-04-16')
      datetime.datetime(2016, 4, 16, 0, 0)
      >>> parse('2019-05-04 18:03:11')
      datetime.datetime(2019, 5, 4, 18, 3, 11)
      >>> parse("Apr 16, 2016 04:05:32 PM")
      datetime.datetime(2016, 4, 16, 16, 5, 32)
      >>> parse('6/7/2016')
      datetime.datetime(2016, 6, 7, 0, 0)
      >>> parse('16/7/2016')
      datetime.datetime(2016, 7, 16, 0, 0)
  • class relativedelta
    년(year), 월(month) 더하고 빼기
    • >>> from dateutil.relativedelta import relativedelta
      >>> from datetime import datetime
      >>> datetime.now()
      datetime.datetime(2019, 5, 4, 18, 5, 23, 624826)
      >>> datetime.now() - relativedelta(years=1)
      datetime.datetime(2018, 5, 4, 18, 5, 31, 567166)
      >>> datetime.now() + relativedelta(years=1)
      datetime.datetime(2020, 5, 4, 18, 5, 45, 334084)
      >>> datetime.now() - relativedelta(months=3)
      datetime.datetime(2019, 2, 4, 18, 5, 51, 619240)
      >>> datetime.now() + relativedelta(months=3)
      datetime.datetime(2019, 8, 4, 18, 5, 58, 367941)
      >>> datetime.now() + relativedelta(days=5)
      datetime.datetime(2019, 5, 9, 18, 9, 37, 71110)
  • rrule 클래스
    두 날짜의 년, 월 수 구하기
    • >>> from dateutil import rrule
      >>> from datetime import date
      >>> dir(rrule)
      ['DAILY', 'FR', 'FREQNAMES', 'HOURLY', 'M365MASK', 'M365RANGE', 'M366MASK', 'M366RANGE', 'MDAY365MASK', 'MDAY366MASK', 'MINUTELY', 'MO', 'MONTHLY', 'NMDAY365MASK', 'NMDAY366MASK', 'SA', 'SECONDLY', 'SU', 'TH', 'TU', 'WDAYMASK', 'WE', 'WEEKLY', 'YEARLY', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_invalidates_cache', '_iterinfo', '_rrulestr', '_thread', 'advance_iterator', 'calendar', 'datetime', 'easter', 'gcd', 'heapq', 'integer_types', 'itertools', 'parser', 'range', 'rrule', 'rrulebase', 'rruleset', 'rrulestr', 'sys', 'warn', 'weekday', 'weekdaybase', 'weekdays']
      >>> diff_months_list = list(rrule.rrule(rrule.MONTHLY, dtstart=date(2016, 10, 1), until=date(2017, 5, 15)))
      >>> diff_months_list
      [datetime.datetime(2016, 10, 1, 0, 0), datetime.datetime(2016, 11, 1, 0, 0), datetime.datetime(2016, 12, 1, 0, 0), datetime.datetime(2017, 1, 1, 0, 0), datetime.datetime(2017, 2, 1, 0, 0), datetime.datetime(2017, 3, 1, 0, 0), datetime.datetime(2017, 4, 1, 0, 0), datetime.datetime(2017, 5, 1, 0, 0)]
      >>> len(diff_months_list)
      8
      >>> diff_years_list = list(rrule.rrule(rrule.YEARLY, dtstart=date(2016, 10, 1), until=date(2020, 5, 15)))
      >>> diff_years_list
      [datetime.datetime(2016, 10, 1, 0, 0), datetime.datetime(2017, 10, 1, 0, 0), datetime.datetime(2018, 10, 1, 0, 0), datetime.datetime(2019, 10, 1, 0, 0)]
      >>> len(diff_years_list)
      4

calendar 모듈

  • function calendar.monthrange(year, month)
    • tuple을 리턴
      • 첫 번째 원소는 month의 1일이 무슨 요일인지(0:월, 1:화...,)
      • 두 번째 원소는 month가 총 며칠 인지
    • >>> import calendar
      >>> calendar.monthrange(2019, 3)
      (4, 31)
      >>> calendar.monthrange(2019, 4)
      (0, 30)