python 微博评论获取的时候日期格式化

# -*- coding: utf-8 -*-
# @Time    : 2018/03/05 10:57
# @Author  : cxa
# @File    : testDataTime.py
# @Software: PyCharm
import datetime
import time
# 今天 12:36
# 34分钟前
# 20秒前
# 2月26日 09:38
#统一格式化成时间类型
def get_format_datetime(datestr):

   now= datetime.datetime.now()
   ymd=now.strftime("%Y-%m-%d")
   y = now.strftime("%Y")
   newdate=now
   if(u"今天" in datestr):
       mdate=time.mktime(time.strptime( ymd+datestr, '%Y-%m-%d今天 %H:%M'))
       newdate=datetime.datetime.fromtimestamp(mdate)
   elif(u"月" in datestr):
       mdate = time.mktime(time.strptime(y + datestr, '%Y%m月%d日 %H:%M'))
       newdate = datetime.datetime.fromtimestamp(mdate)
   elif(u"分钟前" in datestr):
       print(datestr[:-3])
       newdate= now-datetime.timedelta(minutes=int(datestr[:-3]))
   elif (u"秒前" in datestr):
       newdate = now - datetime.timedelta(minutes=int(datestr[:-2]))
   else:
       newdate =datetime.datetime.strptime(datestr, "%Y-%m-%d %H:%M")
   return newdate
print(get_format_datetime("3分钟前"))

  

posted @ 2018-03-05 14:32  公众号python学习开发  阅读(390)  评论(0编辑  收藏  举报