본문 바로가기
Programming/Python

datetime, str, timestamp 변환

by Chan_찬 2021. 1. 13.
728x90
반응형

timestamp, datetime, str

str로 변환

timestamp -> str('2020-11-30 00:00:00')

import time
timestamp = time.time()
dt = str(datetime.fromtimestamp(timestamp))
print(dt)
# 2020-11-30 00:00:00

datetime -> str('2020-11-30 00:00:00')

from datetime import datetime
s = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(s)
# 2020-12-12 00:00:00

timestamp 로 변환

str('2020-11-30 00:00:00') -> timestamp

From date time import date time
import time

src='2020-11-30 00:00:00'
timestamp = time.mktime(datetime.strptime(src, '%Y-%m-%d %H:%M:%S').timetuple())
print(timestamp)
# 1606662000.0

str('2020-11-30 00:00:00') -> datetime

from datetime import datetime
src = '2020-11-30 00:00:00'
dt = datetime.strptime(src, '%Y-%m-%d %H:%M:%S')
print(dt)
# '2020-11-30 00:00:00'

timestamp -> datetime

from datetime import datetime
dt = datetime.fromtimestamp(1606662000)
print(dt)
# 2020-11-30 00:00:00
728x90
728x90
BIG
Buy me a coffeeBuy me a coffee

댓글