1. 程式人生 > >python 對時間的處理

python 對時間的處理

時間處理來源

from datetime import datetime
from datetime import timedelta

if "剛剛" in publish_time:
    publish_time = datetime.now().strftime('%Y-%m-%d %H:%M')

elif "分鐘" in publish_time:
    minute = publish_time[:publish_time.find("分鐘")]
    minute = timedelta(minutes=int(minute))
    publish_time = (
        datetime.now() - minute).strftime(
        "%Y-%m-%d %H:%M")
elif "今天" in publish_time:
    today = datetime.now().strftime("%Y-%m-%d")
    time = publish_time.replace('今天','')
    publish_time = today + " " + time

elif "月" in publish_time:
    year = datetime.now().strftime("%Y")
    publish_time = str(publish_time)
    print publish_time

    publish_time = year + "-" +publish_time.replace('月','-').replace('日','')
else:
    publish_time = publish_time[:16]

print "微博釋出時間: " + publish_time