python 时间转换
def getDateTime(time_str):
'''
转换时间
:param time_str:
:return:
'''
if not isinstance(time_str,unicode):
time_str = time_str.decode('utf-8') time_now = datetime.datetime.now()
time_return = time_str if u'秒' in time_str:
reg = r'(\d+)'
t_second = re.search(reg, time_str)
if t_second is not None:
t_second = t_second.group(1)
t_second = string.atoi(t_second)
t_second = datetime.timedelta(seconds=t_second)
time_return = time_now - t_second
time_return = datetime.datetime.strftime(time_return, '%Y-%m-%d %H:%M:%S')
return time_return
elif u'分钟' in time_str:
reg = r'(\d+)'
t_minute = re.search(reg, time_str)
if t_minute is not None:
t_minute = t_minute.group(1)
t_minute = string.atoi(t_minute)
t_minute = datetime.timedelta(minutes=t_minute)
time_return = time_now - t_minute
time_return = datetime.datetime.strftime(time_return, '%Y-%m-%d %H:%M:%S')
return time_return
elif u'小时' in time_str:
reg = r'(\d+)'
t_hour = re.search(reg, time_str)
if t_hour is not None:
t_hour = t_hour.group(1)
t_hour = string.atoi(t_hour)
t_hour = datetime.timedelta(hours=t_hour)
time_return = time_now - t_hour
time_return = datetime.datetime.strftime(time_return, '%Y-%m-%d %H:%M:%S')
return time_return
elif u'今天' in time_str:
time_return = time_now.strftime('%Y-%m-%d %H:%M:%S')
return time_return
elif u'天前' in time_str:
reg = r'(\d+)'
t_day = re.search(reg, time_str)
if t_day is not None:
t_day = t_day.group(1)
t_day = string.atoi(t_day)
t_day = datetime.timedelta(days=t_day)
time_return = time_now - t_day
time_return = datetime.datetime.strftime(time_return, '%Y-%m-%d %H:%M:%S')
return time_return
elif u'周' in time_str:
reg = u'(\d+)'
t_week = re.search(reg, time_str)
if t_week is not None:
t_week = t_week.group(1)
t_week = string.atoi(t_week)
t_week = datetime.timedelta(weeks=t_week)
time_return = time_now - t_week
time_return = datetime.datetime.strftime(time_return, '%Y-%m-%d %H:%M:%S')
return time_return
elif u'月' in time_str:
reg = r'(\d+)'
t_num = re.search(reg, time_str)
if t_num is not None:
t_num = int(t_num.group(1))
date_to = time_now.month - t_num
# 判断是否跨年
if date_to == 0:
month_to = 12
year_to = -1
else:
month_to = date_to % 12
year_to = (date_to) / 12
# 如果是最后一天31日,注意其他月份没有31日
now_year = time_now.year+year_to
d = calendar.monthrange(now_year,month_to)
now_day = time_now.day
if now_day > d[1]:
now_day = d[1]
date_from = datetime.datetime(now_year,month_to,now_day,time_now.hour,time_now.minute,time_now.second)
time_return = datetime.datetime.strftime(date_from, '%Y-%m-%d %H:%M:%S')
return time_return
elif u'年' in time_str:
reg = u'(\d+)'
t_year = re.search(reg, time_str)
if t_year is not None:
t_year = int(t_year.group(1))
date_from = datetime.datetime(time_now.year-t_year,time_now.month,time_now.day,time_now.hour,time_now.minute,time_now.second)
time_return = datetime.datetime.strftime(date_from, '%Y-%m-%d %H:%M:%S')
return time_return
else:
# time_return = time_str
int_year = time_now.year
str_year = str(int_year)
if time_str.find(str_year) < 0:
time_return = str_year + '-' + time_str
t_count = time_str.count(':')
if t_count == 1:
time_return += ':00'
elif t_count == 0:
time_return += ' 00:00:00' # 如果日期大于当前日期,则年份减1
r_date = datetime.datetime.strptime(time_return, '%Y-%m-%d %H:%M:%S')
if r_date > time_now:
int_year_1 = int_year - 1
time_return = time_return.replace(str_year, str(int_year_1)) return time_return if __name__ == '__main__':
print getDateTime('1周前')
python 时间转换的更多相关文章
- python 时间转换相关
最近需要操作时间的地方相当的多,包括打点,包括时间转换. 罗列最近遇到的两个需求. 1. 关于上篇文章写的base64上传图片的问题,我使用了打点来计算解码需要多少时间.这个对时间精度要求是比较高的. ...
- Python时间,日期,时间戳之间转换,时间转换时间戳,Python时间戳转换时间,Python时间转换时间戳
#1.将字符串的时间转换为时间戳方法: a = "2013-10-10 23:40:00" #将其转换为时间数组 import time timeArray = time.strp ...
- python时间转换
#设a为字符串 import time a = "2011-09-28 10:00:00" #中间过程,一般都需要将字符串转化为时间数组 time.strptime(a,'%Y-% ...
- python时间转换 ticks-FYI
#设a为字符串 import time a = "2011-09-28 10:00:00" #中间过程,一般都需要将字符串转化为时间数组 time.strptime(a,'%Y-% ...
- Python基本时间转换
时间转换 python中处理时间的时候,最常用的就是字符形式与时间戳之间的转换. 把最基本的转换在这里记下来 string -> timestamp import time import dat ...
- python 时间与时间戳之间的转换
https://blog.csdn.net/kl28978113/article/details/79271518 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运 ...
- python 时间和时间戳的转换
对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换 ...
- python—时间与时间戳之间的转换
python-时间与时间戳之间的转换 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块 ...
- UTC,BJT时间转换-python
#UTC,BJT Conversion.py #接收一个BJT时间 bjt = eval(input("输入时间")) #转换 utc = bjt + 2400 - 800 if ...
随机推荐
- JS学习:第一周——NO.3面向对象
[面向对象基础知识] 封装:对于功能相同的代码,我们只需封装一次,以后再遇到类似的功能,只需调用即可,无需重写,避免大量冗余代码. 对象的特征:方法和属性: 面向对象的特点: 封装:低耦合高内聚: 继 ...
- set使用方法
set 添加一个无序的,用set方法,访问速度快,天生解决了重复问题 1.difference 指定某个元素从原来set取出,并生成新的set #difference a = set(["a ...
- mapReduce的shuffle过程
http://www.jianshu.com/p/c97ff0ab5f49 总结shuffle 过程: map端的shuffle: (1)map端产生数据,放入内存buffer中: (2)buffer ...
- 面向对象Part4
---------------------------------------------------------------------------------------------------- ...
- Axel替代wget
Axel替代wget 2011年11月10日admin发表评论阅读评论 Linux下用的最多的下载工具莫过于wget和curl,这两个工具虽然堪称经典.但其单线程的速度越来越不能大软件的下载.于是 ...
- ubuntu-docker-consul-swarm-shipyard-portainer
--- env --- root@node1:~# cat /etc/issueUbuntu 12.04.4 LTS \n \l root@node1:~# docker -vDocker versi ...
- Selenium Xpath Tutorials - Identifying xpath for element with examples to use in selenium
Xpath in selenium is close to must required. XPath is element locator and you need to provide xpath ...
- C#操作XML总结
1.using System.Xml; using System.Xml; //初始化一个xml实例 XmlDocument xml=new XmlDocument(); //导入指定xml文件 xm ...
- 用typedef定义函数指针的问题
在学习windows API的时候,遇到下面这段代码 以前见过的typedef的用法都是给一个数据类型取一个别名 typedef oldTypeName newTypeName 这种给数据类型 ...
- hdu 5384 Danganronpa
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 思路:没学自动机时以为是道KMP然后就tle了好几把,AC自动机模板题 #include<cs ...