python datetime 사용방법

2019.06.21 11:44python

python datetime 사용법

import datetime

# UTC datetime
print(datetime.datetime.utcnow())
# result : 2019-06-21 02:38:05.099542

# 포멧 변경
print(datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%H:%S.%f')[:-2])
# result : 2019-06-21 02:02:05.0995

# 오늘 날짜
print(datetime.date.today())  # result : 2019-06-21
# 오늘 날짜 타입 확인
print(type(datetime.date.today()))  # result<class 'datetime.date'>

# 오늘 날짜 타입 Str 변환
print(datetime.date.today().strftime('%Y-%m-%d '))  # result : 2019-06-21

# 오늘 날짜 타입 확인
print(type(datetime.date.today().strftime('%Y-%m-%d ')))  # <class 'str'>

# str convert to date
oneDatetime = datetime.datetime.strptime('2019-06-20 14:15:00', '%Y-%m-%d %H:%M:%S')
print(oneDatetime)  # 2019-06-20 14:15:00

# date replace
myDatetime = datetime.datetime.strptime('2018-07-28 12:11:32', '%Y-%m-%d %H:%M:%S')
print(myDatetime)  # >2018-07-28 12:11:32
yourDatetime = myDatetime.replace(day=27)
print(yourDatetime)  # > 2018-07-27 12:11:32

# 현재 시간으로 TIME 추출
thisTime = datetime.datetime.now().time()
print(thisTime)  # > 11:41:35.287564

# 타임 지정
standardTime = datetime.time(14, 36, 00, 0)
print(standardTime)  # > 14:36:00

# 날짜 비교 
if thisTime <= standardTime:
    print("시작")

'python' 카테고리의 다른 글

selenuim 과 requests 비교  (0) 2019.06.25
selenium 웹크롤링  (0) 2019.06.21
python datetime 사용방법  (0) 2019.06.21
python 기본 함수 time  (0) 2019.06.21
python random 함수  (0) 2019.06.21
python centos 설치  (0) 2019.06.21
1 2 3 4 5 6