Python/날짜 관련 datetime



1. Today #

from datetime import datetime

date = f"{datetime.now():%Y-%m-%d}" # today

2018-11-03

2. 시간 더하고 빼기 timedelta #

from datetime import datetime, timedelta

yesterday = datetime.now() - timedelta(days=1)

print (datetime.now())
print (yesterday)

2018-11-03 12:57:10.385943
2018-11-02 12:57:10.385943

3. Epoch & Unix Timestamp Conversion #

from datetime import datetime

time = 1541217027247
dt = datetime.utcfromtimestamp(time / 1000.)
date = f"{dt.now():%Y-%m-%d %H:%M:%S}"

2018-11-03 12:51:30

4. Get an UTC #

import pytz
from datetime import datetime, timedelta

print (datetime.now(pytz.utc).strftime("%Y-%m-%d %H:%M:%S"))

2018-11-03 03:51:30

이 글에는 0 개의 댓글이 있습니다.