day05 -2- 标准库详解之time 与 datatime模块

1、Python上的时间表示方式 :


首先我们一个先知道Python上的时间有哪几种表示形式:


import time,datetime    # 第一种 标准时间格式
>>> print(time.asctime())
Sun Apr 22 21:47:29 2018
>>>
>>> t = time.time()  # 2第二种 时间戳  (从1970年0点0分0秒到目前的秒数) :
>>>t
1524365536.642032
>>> t/3600/24/365
48.33731536129933
>>> 1970+48
2018
>>> time.localtime()   # 3 第三种 以元组的形式
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=22, tm_hour=21, tm_min=48, tm_sec=9, tm_wday=6, tm_yday=112, tm_isdst=0)
>>>

即以下三种:



2、time模块详解(三种时间表示形式之间的转换)


1、gmtime() -- convert seconds since Epoch to UTC tuple

  >>> time.gmtime() #把当前时间的时间戳传入,转换为标准时间,以            元组形式打印出来
        time.struct_time(tm_year=2018, tm_mon=4, tm_mday=22, t                m_hour=3, tm_min=7, 
        tm_sec=56, tm_wday=6, tm_yday=112, tm_isdst=0)


2、localtime() -- convert seconds since Epoch to local time tuple   

>>> time.localtime() #把当前时间的时间戳传入,转换为当地时间,以元组形式打印出来
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=22, tm_hour=11, tm_min=20, tm_sec=50, tm_wday=6, tm_yday=112, tm_isdst=0)
>>> time.localtime(1253123613)  # 任意输入一个时间戳,转换为当地时区
time.struct_time(tm_year=2009, tm_mon=9, tm_mday=17, tm_hour=1, tm_min=53, tm_sec=33, tm_wday=3, tm_yday=260, tm_isdst=0)
>>>  1253123613 /3600/24/365
39.736289098173515
>>> 1970+39        # 校验年份
2009
>>> x = time.localtime()
>>> x.tm_year   #取出年份
2018
>>>
>>> x.tm_yday   #获取当前的一年的天数
112


 3、strftime() -- convert time tuple to string according to format specification

x = time.localtime() #把元组的时间转换为以字符串打印出标准格式
>>> x
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=22, tm_hour=11, tm_min=35, tm_sec=13, tm_wday=6, tm_yday=112, tm_isdst=0)
>>> time.striftime("%Y-%m: %H:%M:%S",x)
>>> time.strftime("%Y-%m: %H:%M:%S",x)
'2018-04: 11:35:13'
>>>


4、strptime("格式化的字符串",struct_time) -- parse string to time tuple according to

#format specification  ,格式化的字符串与时间格式一一对应,有强关联

>>> time.strptime('2018-04: 11:35:13',"%Y-%m: %H:%M:%S") #将字符串格式的标准时间转换为元组的
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=1, tm_hour=11, tm_min=35, tm_sec=13, tm_wday=6, tm_yday=91, tm_isdst=-1)
>>>


5、asctime() -- convert time tuple to string  把结构化的元组的形式转换为asctime的格式,接受的变量为元组的形式

   ctime(seconds) -> string

    Convert a time in seconds since the Epoch to a string in local time.
    This is equivalent to asctime(localtime(seconds)). When the time tuple is
    not present, current time as returned by localtime() is used.

>>> time.asctime()

'Sun Apr 22 18:20:55 2018'


6、ctime() -- convert time in seconds to string :把时间戳转换为元组的形式,接收的变量为时间戳的形式

>>> time.asctime()
'Sun Apr 22 18:20:55 2018'
>>> time.ctime()
'Sun Apr 22 18:24:40 2018'
>>>

总结:


3、datetime模块详解

datetime是对time进一步的封装

1 、获取当前时间:

>>> datetime.datetime.now()
datetime.datetime(2018, 4, 22, 18, 29, 1, 207753)
>>> 

2、获取当前时间前后 n 天的时间及 前后 n 个小时的时间:
>>> datetime.datetime.now() + datetime.timedelta(3) #获取当前时间前3 天的时间
datetime.datetime(2018, 4, 25, 18, 32, 28, 934855)        
>>> datetime.datetime.now() + datetime.timedelta(-3)  #获取当前时间后 3天的时间
datetime.datetime(2018, 4, 19, 18, 32, 56, 517233)
>>>
>>> datetime.datetime.now() + datetime.timedelta(hours=3)  #获取当前时间前3个小时的时间
datetime.datetime(2018, 4, 22, 21, 34, 7, 558471)
>>> datetime.datetime.now() + datetime.timedelta(hours=-3) #获取当前时间后3个小时的时间
datetime.datetime(2018, 4, 22, 15, 34, 14, 822568)
>>>
DirectiveMeaningNotes
%aLocale’s abbreviated weekday name. 
%ALocale’s full weekday name. 
%bLocale’s abbreviated month name. 
%BLocale’s full month name. 
%cLocale’s appropriate date and time representation. 
%dDay of the month as a decimal number [01,31]. 
%HHour (24-hour clock) as a decimal number [00,23]. 
%IHour (12-hour clock) as a decimal number [01,12]. 
%jDay of the year as a decimal number [001,366]. 
%mMonth as a decimal number [01,12]. 
%MMinute as a decimal number [00,59]. 
%pLocale’s equivalent of either AM or PM.(1)
%SSecond as a decimal number [00,61].(2)
%UWeek number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.(3)
%wWeekday as a decimal number [0(Sunday),6]. 
%WWeek number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.(3)
%xLocale’s appropriate date representation. 
%XLocale’s appropriate time representation. 
%yYear without century as a decimal number [00,99]. 
%YYear with century as a decimal number. 
%zTime zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. 
%ZTime zone name (no characters if no time zone exists). 
%%A literal '%' characte



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值