alex_bn_lee

导航

【310】◀▶ Python 日期和时间

参考:


01   datetime.datetime 包含 date 对象和 time 对象的所有信息。
02   datetime.date 包含年月日。
03   datetime.time 包含一天的时分秒信息。
04   datetime.timedelta
用来指定一个时间间隔,表示两个日期或者时间的不同。
05   time 模块  

序号 类名称  

功能说明

  语法 & 举例
01 datetime.datetime 对象  

====<<<< Description>>>>====

datetime 模块下 的 datetime 对象,包含 date 对象和 time 对象的所有信息。
----------------------------------------------------------------------------------

====<<<< Syntax >>>>====

datetime.datetime (year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
----------------------------------------------------------------------------------

====<<<< Parameters >>>>====

◈  year:必须。MINYEAR <= year <= MAXYEAR
◈  month:必须。1 <= month <= 12
◈  day:必须。1 <= day <= 365/366
◈  hour:默认 0。0 <= hour < 24
◈  minute:默认 0。0 <= minute < 60
◈  second:默认 0。0 <= second < 60
◈  microsecond:默认 0。0 <= microsecond < 1000000
----------------------------------------------------------------------------------

====<<<< Methods >>>>====

◈  datetime.today ():返回现在的当地时间。
◈  datetime.now (tz=None):返回本地当前的日期和时间。如果可选的参数 tz 为 None 或者没有指定,就如同today()。
◈  datetime.utcnow ():返回现在的 UTC 时间。

◈  datetime.date ():返回相同年月日的 date 对象。
◈  datetime.time ():返回相同时分秒的 time 对象。
◈  datetime.replace (year=self.year, month=self.month, day=self.day, hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, * fold=0):返回一个除了发生变化的属性外其他一样的 datetime 。
◈  datetime.weekday ():返回一个整型,Monday 是 0,Sunday 是 6,一周中的第几天。

◈  datetime.timetuple ():返回一个结构体,里面包含如下:time.struct_time(tm_year=2017, tm_mon=2, tm_mday=12, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=43, tm_isdst=-1)。其中 tm_yday 为一年中的第几天。
----------------------------------------------------------------------------------

====<<<< Attributes >>>>====

◈  datetime.min:返回值为 datetime,最小的。
◈  datetime.max:返回值为 datetime,最大的。
◈  datetime.year:年
◈  datetime.month:月
◈  datetime.day:日
◈  datetime.hour:时
◈  datetime.minute:分
◈  datetime.second:秒
◈  datetime.microsecond:微秒

 
>>> import datetime

# 调用日期信息
>>> d1 = datetime.datetime.today()
>>> print(d1)
2018-04-14 22:34:59.486000
>>> d1.year
2018
>>> d1.month
4
>>> d1.day
14
>>> d1.date()
datetime.date(2018, 4, 14)
>>> d1.time()
datetime.time(22, 34, 59, 486000)

# 日期计算
>>> d2 = d1.replace(year=2019, month=2, day=3)
>>> d2
datetime.datetime(2019, 2, 3, 22, 34, 59, 486000)
>>> dd = d2 - d1
>>> dd.days
295

# 构建日期
>>> d3 = datetime.datetime(2007, 9, 1)
>>> d3
datetime.datetime(2007, 9, 1, 0, 0)

# 一年中的第几天
>>> today = datetime.datetime.today()
>>> today.timetuple()
time.struct_time(tm_year=2018, tm_mon=6, tm_mday=1, tm_hour=10, 
tm_min=34, tm_sec=37, tm_wday=4, tm_yday=152, tm_isdst=-1)
>>> today.timetuple().tm_yday
152

根据 string 来创建 datetime,通过 datetime.strptime() 实现

下面代码读取格式如下的文本 “2019-11-10 09:08:07”

# "%Y-%m-%d %H:%M:%S"
# 以上为文本格式

ws = []
fn = r"D:\OneDrive - UNSW\tweets_flu.csv"
df = pd.read_csv(fn)
for i in range(len(df)):
    t = df.iloc[i]['created_at']
    w = datetime.strptime(t, "%Y-%m-%d %H:%M:%S").strftime("%W")
    ws.append(w)

 


 

 02 datetime.date 对象  

====<<<< Description>>>>====

datetime 模块下 的 date 对象,包含年月日。
----------------------------------------------------------------------------------

====<<<< Syntax >>>>====

datetime.date (year, month, day)
----------------------------------------------------------------------------------

====<<<< Parameters >>>>====

◈  year:必须。MINYEAR <= year <= MAXYEAR
◈  month:必须。1 <= month <= 12
◈  day:必须。1 <= day <= 365/366
----------------------------------------------------------------------------------

====<<<< Methods >>>>====

◈  date.today ():返回现在的当地时间。
◈  date.replace (year=self.year, month=self.month, day=self.day):返回一个除了发生变化的属性外其他一样的 date 。
◈  date.weekday ():返回一个整型,Monday 是 0,Sunday 是 6,一周中的第几天。
----------------------------------------------------------------------------------

====<<<< Attributes >>>>====

◈  date.min:返回值为 date,最小的。
◈  date.max:返回值为 date,最大的。
◈  date.year:年
◈  date.month:月
◈  date.day:日

 
>>> d1 = datetime.date.today()
>>> d1.year
2018
>>> d2 = d1.replace(month=4, day=20)
>>> d2
datetime.date(2018, 4, 20)
>>> d1
datetime.date(2018, 4, 14)
>>> (d2-d1).days
6
>>> d3 = datetime.date(2018,5,3)
>>> d3
datetime.date(2018, 5, 3)

 

03 datetime.time 对象  

====<<<< Description>>>>====

datetime 模块下 的 time 对象,包含一天的时分秒信息。
----------------------------------------------------------------------------------

====<<<< Syntax >>>>====

datetime.time (hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
----------------------------------------------------------------------------------

====<<<< Parameters >>>>====

◈  hour:默认 0。0 <= hour < 24
◈  minute:默认 0。0 <= minute < 60
◈  second:默认 0。0 <= second < 60
◈  microsecond:默认 0。0 <= microsecond < 1000000
----------------------------------------------------------------------------------

====<<<< Methods >>>>====

◈  datetime.replace (hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, * fold=0):返回一个除了发生变化的属性外其他一样的 time 。
----------------------------------------------------------------------------------

====<<<< Attributes >>>>====

◈  time.min:返回值为 time,最小的。
◈  time.max:返回值为 time,最大的。
◈  time.hour:时
◈  time.minute:分
◈  time.second:秒
◈  time.microsecond:微秒

   
 04  datetime.timedelta 对象  

====<<<< Description>>>>====

datetime 模块下 的 datedelta 对象,用来指定一个时间间隔,表示两个日期或者时间的不同。
----------------------------------------------------------------------------------

====<<<< Syntax >>>>====

datetime.timedelta (days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
----------------------------------------------------------------------------------

====<<<< Parameters >>>>====

◈  hour:默认 0。0 <= hour < 24
◈  minute:默认 0。0 <= minute < 60
◈  second:默认 0。0 <= second < 60
◈  microsecond:默认 0。0 <= microsecond < 1000000
----------------------------------------------------------------------------------

====<<<< Attributes >>>>====

◈  timedelta.days:天数
◈  timedelta.seconds:秒数
◈  timedelta.microseconds:微秒数

 
>>> d1 = datetime.date(1987,8,31)
>>> d2 = datetime.date.today()
>>> dd = d2 - d1
>>> dd.days
11184

>>> d1 = datetime.datetime(1987,8,31)
>>> d2 = datetime.datetime.today()
>>> dd = d2 - d1
>>> dd.days
11184
05 time 模块  

参考:Python 日期和时间

time 模块 可以用于格式化日期和时间。
----------------------------------------------------------------------------------

====<<<< Methods >>>>====
◈  time.asctime ( [t] ):字符串显示时间。'Fri Jun 08 16:55:40 2018'
◈  time.gmtime ( [secs] ):UTC 时间,返回值为 struct_time 。    
  tm_year=2018
  tm_mon=6
  tm_mday=8
  tm_hour=13
  tm_min=42
  tm_sec=58
  tm_wday=4
  tm_yday=159
  tm_isdst=0,Daylight Saving Time,是否夏令时

◈  time.localtime ( [secs] ):当地时间,,返回值为 struct_time
◈  time.strftime ( format[, t] ):格式化字符串显示时间。
◈  time.strptime ( string[, format] ):解析时间字符串,返回值为 struct_time 。字符串格式为 "%a %b %d %H:%M:%S %Y"。
◈  time.sleep ( secs ):暂停的秒数。(可以是小数)

   
# 自动识别是本世纪还是上个世纪
>>> time.strptime("12,11,15", "%y,%m,%d")
time.struct_time(tm_year=2012, tm_mon=11, tm_mday=15, tm_hour=0, 
tm_min=0, tm_sec=0, tm_wday=3, tm_yday=320, tm_isdst=-1)
>>> time.strptime("97 Jun 8", "%y %b %d")
time.struct_time(tm_year=1997, tm_mon=6, tm_mday=8, tm_hour=0, 
tm_min=0, tm_sec=0, tm_wday=6, tm_yday=159, tm_isdst=-1)
>>> time.strptime("30 Nov 00", "%d %b %y")
time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, 
tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)
>>> time.localtime()
time.struct_time(tm_year=2018, tm_mon=6, tm_mday=8, tm_hour=13, 
tm_min=42, tm_sec=58, tm_wday=4, tm_yday=159, tm_isdst=0)
>>> time.gmtime()
time.struct_time(tm_year=2018, tm_mon=6, tm_mday=8, tm_hour=5, 
tm_min=43, tm_sec=19, tm_wday=4, tm_yday=159, tm_isdst=0)
>>> time.localtime().tm_yday
159
>>> time.asctime()
'Fri Jun 08 16:55:40 2018'
>>> time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
'Fri, 08 Jun 2018 17:02:18 +0000'

 

           

将字符串转换为 datetime

参考:Converting string into datetime

参考:strftime() and strptime() Behavior

代码:(strptime 就是 string parse time)

from datetime import datetime

a = "Wed Oct 10 20:19:24 +0000 2018"

b = datetime.strptime(a, "%a %b %d %H:%M:%S %z %Y")

print(b.year, b.month, b.day, b.hour, b.minute, b.tzinfo)

# output
# 2018 10 10 20 19 UTC

 

posted on 2018-04-14 14:41  McDelfino  阅读(370)  评论(0编辑  收藏  举报