参考:


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

【310】◀▶ Python 日期和时间的更多相关文章

  1. Python 日期和时间(转)

    Python 日期和时间 Python程序能用很多方式处理日期和时间.转换日期格式是一个常见的例行琐事.Python有一个 time 和 calendar 模组可以帮忙. 什么是Tick? 时间间隔是 ...

  2. (转)Python 日期和时间

    转自http://www.runoob.com/python/python-date-time.html Python 日期和时间 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见 ...

  3. Python 日期和时间 —— datetime

    Python 日期和时间 —— datetime Python提供了多个内置模块用于操作日期时间,如calendar,time,datetime.calendar用于处理日历相关 :time提供的接口 ...

  4. python 日期、时间、字符串相互转换

    python 日期.时间.字符串相互转换 在python中,日期类型date和日期时间类型dateTime是不能比较的. (1)如果要比较,可以将dateTime转换为date,date不能直接转换为 ...

  5. Python 日期和时间_python 当前日期时间_python日期格式化

    Python 日期和时间_python 当前日期时间_python日期格式化 Python程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 cal ...

  6. Python日期和时间_什么是Tick_什么是时间元组_获取当前时间

    Python 日期和时间_什么是 Tick _什么是时间元组: 时间和日期:某年某月某日某时某分某秒 Tick: 时间间隔以 秒 为单位的浮点小数,起始时间为:1970年1月1日0点0分开始 # Ti ...

  7. 【转】Python 日期和时间

    本文转自:http://www.runoob.com/python/python-date-time.html Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Pytho ...

  8. Python 日期和时间

    Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时间间隔是以秒为单位的浮点小数. ...

  9. Python 日期和时间操作

    Python提供了一个time 和calendar模块可以用于格式化日期和时间. 时间间隔是以秒为单位的浮点小数. 每个时间戳都是以自从1970年1月1日午夜(历元)经过了多长时间来表示. Pytho ...

随机推荐

  1. 轻量级KVO[译]

        在这篇文章中,我会实现一个自己用的简单KVO类,我认为KVO非常棒,然而对于我大部分的使用场景来说,有这两个问题: 1. 我不喜欢在observeValueForKeyPath:ofObjec ...

  2. 剑指offer第七章&第八章

    剑指offer第七章&第八章 1.把字符串转换成整数 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一个合法的数值则返回0 输入描述: 输入一个字符串 ...

  3. python笔记-12 redis缓存

    一.redis引入 1.简要概括redis 1.1 redis默认端口:6379 1.2 redis实现的效果:资源共享 1.3 redis实现的基本原理:不同的进程和一个公共的进程之间建立socke ...

  4. callback回调函数-python

    链接:http://www.zhihu.com/question/19801131/answer/27459821来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 编程分 ...

  5. BZOJ4547 Hdu5171 小奇的集合

    题意 有一个大小为n的可重集S,小奇每次操作可以加入一个数a+b(a,b均属于S),求k次操作后它可获得的S的和的最大值.(数据保证这个值为非负数) 对于100%的数据,有 n<=10^5,k& ...

  6. iPhone 和 Galaxy高速拍照原理具体分析

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zoosenpin/article/details/30027263 1 原理分析 首先我们看一下An ...

  7. vi配置

    1.配置文件的位置在目录 /etc/ 下面,有个名为vimrc的文件,这是系统中公共的vim配置文件,对所有用户都有效.而在每个用户的主目录下,都可以自己建立私有的配置文件,命名为:“.vimrc”. ...

  8. 莫名其妙的js基础学习!

    JavaScript基本组成部分: 1,ECMAScript:js的语法标准,基本的变量,运算符,函数,if语句,for语句等 2,DOM:操作网页上的元素API,比如盒子的移动,变色,轮播图等. 3 ...

  9. 使用AWK分析Oracle系统锁定、Hang状态

    在早期Oracle版本中,由于技术不成熟等原因,数据库锁定和僵死状态还是时有发生的.对待这些问题,老先生们的处理策略无外乎是“重启”和“考究”两种策略.所谓“重启”,通过强制的重启服务器或者数据库,将 ...

  10. 安装QConfig备忘

    下载wget https://github.com/Qihoo360/QConf/archive/1.2.1.tar.gz 解压tar -zxf 1.2.1.tar.gz进入目录cd QConf-1. ...