Daily Develope

[Python] 시간 변환 (datetime) 본문

Develope/Python

[Python] 시간 변환 (datetime)

noggame 2022. 12. 5. 15:24

생성

  • 현재시간 생성
from datetime import date

today:datetime = date.today()

변환

X to Datetime

  • timestamp to datetime
from datetime import datetime

...

datetime.fromtimestamp(timestamp_value).strftime("%Y/%m/%d %H:%M:%S")
# output : 2000/01/01 12:34:56
  • string to datetime
from datetime import datetime
from datetime import date

today_str = "2000 01 01"
today = datetime.strptime(today_str, "%Y %m %d")

# print(today) : 2000 01 01
  • datetime to datetime (포맷 변환)

Datetime to String

from datetime import datetime
from datetime import date

today:datetime = date.today()
today_str = today.strftime("%Y %m %d")

# print(today_str) : 2000 01 01

시간 기술 포맷

Python docs. : strftime-and-strptime-format-codes

Comments