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 ...
随机推荐
- 对前台传回的list进行分割,并放在sql语句的in中
前端数据集传回数据 var matDeptHisMonthPlanStore = Ext.data.StoreManager.lookup('matDeptHisMonthPlanStore'); m ...
- MySQL 查询所有子级函数
BEGIN DECLARE sChildList VARCHAR(4000); DECLARE sChildTemp VARCHAR(4000); SET sChildTemp =cast(rootI ...
- getPx function
function getPX(str){ return str.substring(0,str.indexOf('px'));}
- PHP基础
$a=10; //$b="hello";//$a=(string)$a; 强制转换A的类型为字符串 //settype($a,"string");//var_d ...
- Unity3D配合AndroidStudio打包
SET UNITY_PATH="C:\Program Files\Unity\Editor\Unity.exe" echo UNITY_PATH=%UNITY_PATH% SET ...
- SAP 订单状态跟踪
*&--------------------------------------------------------------------- *& Program name: *& ...
- 让一个图片在div中居中(四种方法)
第一种方法: <div class="title"> <div class="flag"></div> <div cl ...
- 关于Python对齐问题
最近在学习父与子的编程之旅,书上有一个关于猜数的游戏代码,自己敲了以后老是不对,仔细检查后发现是对齐问题. 废话不说了,直接上图: 上面是正确的,下面这个是有问题的,大家可以看下Python代码如果没 ...
- 如何在Linux中搭建禅道8.4.1(httpd+php+mysql)
1.安装httpd 命令:yum install httpd 然后一路y即可 2.安装php 命令:yum install php 3.安装php-mysql 命令:yum install php ...
- iOS常用系统信息获取方法
一.手机电量获取,方法二需要导入头文件#import<objc/runtime.h> 方法一.获取电池电量(一般用百分数表示,大家自行处理就好) -(CGFloat)getBatteryQ ...