본문 바로가기
IT, 프로그램, SW개발/Python,파이썬, AI

[Python/파이썬] 파이썬 시간변환

by RedBaDa 2023. 3. 7.

 

import time

curTime = time.time()
cTime = time.ctime(curTime)
gmTime = time.gmtime(curTime)
mkTime = time.mktime(gmTime)

print "Second :", curTime
print "DateTime :", cTime
print "TimeStruct :", gmTime
print "TimeStruct To Second :", mkTime

결과

Second : 1447998891.9
DateTime : Fri Nov 20 14:54:51 2015
TimeStruct : time.struct_time(tm_year=2015, tm_mon=11, tm_mday=20, tm_hour=5, tm_min=54, tm_sec=51, tm_wday=4, tm_yday=324, tm_isdst=0)
TimeStruct To Second : 1447966491.0

 

 

Timestamp to datetime

import time, datetime
dateTime = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
print(dateTime)

결과

'2016-08-09 00:04:41'

 

 

날짜 차이 구하기

from datetime import datetime
print((datetime.strptime('20070515','%Y%m%d').date() - datetime.strptime('20070501','%Y%m%d').date()).days)

결과

14

반응형