본문 바로가기

언어 꿀Tip/Python 꿀tip!

07_01_55. 날짜, 시간 함수 strptime , strftime

  • 날짜, 시간 값을 문자열로 출력
    • strftime 함수 이용
import pandas as pd
import datetime

 ## 날짜, 시간을 문자열로 출력  => strftime
 
now = datetime.datetime.now()
print(now)

nowdate = now.strftime('%Y-%m-%d')
print(nowdate)

nowtime = now.strftime('%H:%M:%S')
print(nowtime)

print(type(nowdate))

 

  • 문자열로부터 날짜와 시간 정보를 읽어서 datetime.datetime 객체 생성
    • strptime 함수 이용
date = datetime.datetime.strptime('2020-06-21 12:25:39', '%Y-%m-%d %H:%M:%S')
print(date)
print(type(date))

dt = date.strftime('%Y%m%d')
print(dt)