android 时间戳转换成日期_python常用时间处理方法

在实际数据分析的工作中,经常能遇到含有时间的数据,尤其是时间序列问题。python中有专门的时间模块用于处理时间问题。常用的有time模块和datetime模块。

一、time模块

1、time.time()返回时间戳(自1970-1-1 0:00:00 至今的秒数)

import time# 获取当前的时间戳
t1 = time.time()
print(t1)# 输出为:1547966767.9868093

2、time.localtime()将时间戳转换为struct_time对象。

struct_time对象有9个属性,分别为:

tm_year:年份,如 2019tm_mon:月份,取值范围为[1, 12]tm_mday: 一个月中的第几天,取值范围为[1-31]tm_hour:小时, 取值范围为[0-23]tm_min:分钟,取值范围为[0, 59]tm_sec: 秒,取值范围为[0, 59]tm_wday: 一个星期中的第几天,取值范围为[0-6],0表示星期一tm_yday: 一年中的第几天,取值范围为[1, 366]tm_isdst:是否为夏令时,可取值为:0(指定为非夏令时) , 1(指定为夏令时) 或 -1(由系统判断)

用法如下:

import time# 获取当前struct_time格式的时间
time.localtime() # 输出为:time.struct_time(tm_year=2019, tm_mon=1, tm_mday=20, tm_hour=14, tm_min=27, tm_sec=15, tm_wday=6, tm_yday=20, tm_isdst=0)
# 将时间(以秒为单位的时间戳)转换为struct_time格式
time.localtime(1547965473) # 输出:time.struct_time(tm_year=2019, tm_mon=1, tm_mday=20, tm_hour=14, tm_min=24, tm_sec=33, tm_wday=6, tm_yday=20, tm_isdst=0)# 将当前时间戳转换为struct_time格式

3、time.mktime() 将struct_time对象实例转换成时间戳

time.mktime(time.localtime())# 输出:1547967475.0

4、time.ctime() 将一个时间戳转换为一个24个字符的时间字符串

time.ctime(1547967475.0) # 输出:'Sun Jan 20 14:57:55 2019'
time.ctime() # 将当前时间准换为字符串格式

5、time.asctime()将struct_time形式的时间转换为一个24个字符的时间字符串

time.asctime(time.localtime())# 输出:'Sun Jan 20 15:21:42 2019'

6、time.strptime(time_string, time_format)将时间字符串转换为struct_time时间对象,其中:time_string是时间字符串,如‘2019-01-20 15:31’,time_format是时间字符串的格式, 如前面的时间字符串格式为'%Y-%m-%d %H:%M'

time.strptime('2019-01-20 15:40:34', '%Y-%m-%d %H:%M:%S')time.strptime('2019/01/20 15:40:34', '%Y/%m/%d %H:%M:%S')time.strptime('20190120154034', '%Y%m%d%H%M%S')time.strptime('Sun Jan 20 15:40:34 2019')time.strptime('Sun Jan 20 15:40:34 2019', '%a %b %d %H:%M:%S %Y') # 默认格式,同上

7、time.strftime(time_format, struct_time)将struct_time格式时间转换成时间字符串,其中:struct_time是struct_time格式时间,time_format是时间字符串的格式

time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())# 输出:'2019-01-20 16:02:14'
time.strftime('%Y/%m/%d %H:%M:%S', time.localtime())# 输出:'2019/01/20 16:03:57'

二、datetime模块

datatime模块中有几个常用的类,datetime.date类、datetime.time类、datetime.datetime类、datetime.timedelta类等。

1、datetime.date类,表示日期,年、月、日等

(1)类方法:

datetime.date.today():返回当前日期的date对象

import datetimedatetime.date.today()# 输出:datetime.date(2019, 1, 20)

datetime.date.fromtimestamp():将戳转换为一个datetime.date对象

datetime.date.fromtimestamp(time.time())# 输出:datetime.date(2019, 1, 20)

(2)对象方法

首先,建一个datetime.date类的对象:

d = datetime.date(2019, 1, 20)# d为:datetime.date(2019, 1, 20)

常用的对象方法:

d.year; d.month; d.day  # 分别返回年、月、日 
d.timetuple() # 返回日期对应的time.struct_time格式时间# 输出:time.struct_time(tm_year=2019, tm_mon=1, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=20, tm_isdst=-1)
d.isoformat() # 返回'YYYY-MM-DD'格式的日期字符串# 输出:'2019-01-20'  
d.strftime('%Y/%m/%d') # 返回指定格式的日期字符串# 输出:'2019/01/20'

2、datetime.time类,表示时间,时、分、秒、微秒等

常用的对象方法:

t = datetime.time(21, 11, 34, 3452)
t.hour; t.minute; t.second; t.microsecond # 分别返回时、分、秒、微秒
t.isoformat() # 返回一个'HH:MM:SS.%f'格式的时间字符串# 输出:'21:21:34.003452'
t.strftime('%H%M%S') # 返回指定格式的时间字符串# 输出:'212134'
t.strftime('%H%M%S.%f')# 输出:'212134.003452'

3、datetime.datetime类,表示日期时间,年、月、日、时、分、秒、微秒等

(1)类方法

datetime.datetime.today()返回当前日期时间的datetime对象

datetime.datetime.today()# 输出:datetime.datetime(2019, 1, 20, 21, 21, 7, 977045)

datetime.datetime.fromtimestamp()将时间戳转换为datetime.datetime对象

datetime.datetime.fromtimestamp(time.time())# 输出:datetime.datetime(2019, 1, 20, 21, 47, 51, 12045)

datetime.datetime.strptime(datetime_string, datetime_format)将日期时间字符串转换为datetime.datetime对象

datetime.datetime.strptime('2019/01/20 21:49', '%Y/%m/%d %H:%M')# 输出:datetime.datetime(2019, 1, 20, 21, 49)

datatime.datetime.combine(datatime.date, datatime.time)将指定的datatime.date和datatime.time对象整合成一个datetime.datatime对象

datetime.datetime.combine(datetime.date(2019, 1, 20), datetime.time(21, 55, 45))# 输出:datetime.datetime(2019, 1, 20, 21, 55, 45)

(2)对象方法

建一个datetime.datetime类的对象:

dt = datetime.datetime(2019, 1, 20, 22, 13, 34, 57483)# dt为:datetime.datetime(2019, 1, 20, 22, 13, 34, 57483)

方法如下:

dt.year;dt.month;dt.day;dt.hour;dt.minute;dt.second;dt.microsecond # 返回年、月、日、时、分、秒、毫秒
dt.date() # 获取datetime.datetime对象对应的datetime.date对象
dt.time() # 获取datetime.datetime对象对应的datetime.time对象
dt.timestamp() # 转换为时间戳
dt.timetuple() # 转换为time.struct_time对象
dt.isoformat(sep) # 返回一个时间字符串,sep是分隔符
dt.isoformat(sep=' ')# 输出:'2019-01-20 22:13:34.057483'
dt.isoformat(sep='/')# 输出:'2019-01-20/22:13:34.057483'
dt.strftime(string_format) # 返回指定格式的时间字符串
dt.strftime('%Y%m%d %H:%M:%S.%f')# 输出:'20190120 22:13:34.057483'

4、datetime.timedelta代表两个时间之间的时间差

(1)timedelta内部只存储days,seconds,microseconds

# 可以有以下参数,但最后只以days,seconds,microseconds形式存储
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=1, minutes=0, hours=2, weeks=1)# 如下:
datetime.timedelta(days=1, seconds=2, microseconds=345, milliseconds=1, minutes=0, hours=2, weeks=1)# 输出:datetime.timedelta(8, 7202, 1345)

(2)两个date或datetime对象相减可以返回一个timedelta对象

T1 = datetime.datetime(2019, 1, 20, 22, 13, 34, 57483)
T2 = datetime.datetime(2018, 2, 20, 22, 44, 34, 67883)
T3 = datetime.date(2018, 2, 20)
T4 = datetime.date(2018, 1, 10)
T1-T2# 输出:datetime.timedelta(333, 84539, 989600)
T3-T4# 输出:datetime.timedelta(41)

(3)date或datetime对象加减timedelta对象返回date或datetime对象

now = datetime.datetime(2019, 1, 22, 0, 17, 34, 1234)
delta = datetime.timedelta(days=1, seconds=2, hours=2)# 1天2小时2秒前
now - delta# 输出:datetime.datetime(2019, 1, 20, 22, 17, 32, 1234)
fcb2ed2743d13c54b7c5f0fd76b6f1e0.gif
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值