参考:


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. bzoj 1858 序列操作

    bzoj 1858 序列操作 带有随机多个区间单值覆盖的区间操作题,可考虑用珂朵莉树解决. #include<bits/stdc++.h> using namespace std; #de ...

  2. FastJson的一些使用

    前言 最近经常使用json的一些转换,使用的是fastjson,所以就对fastjson进行了一些汇总,记录下来. 正文 主要的api 首先是一些类库的说明: SerializeWriter:相当于S ...

  3. Java 自定义FTP连接池

    转自:https://blog.csdn.net/eakom/article/details/79038590 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...

  4. fortran语法笔记

    1,数据类型,fortran支持整形,real型,logical型,char型,复数型.整形分为为长整形和短整形定义长整形的方法 同时声明多个变量的话可以用逗号隔开. 加两个冒号的话可以直接在声明的时 ...

  5. 【转】一步一步教你在Ubuntu12.04搭建gstreamer开发环境

    原文网址:http://blog.csdn.net/xsl1990/article/details/8333062 闲得蛋疼    无聊寂寞冷    随便写写弄弄 看到网上蛮多搭建gstreamer开 ...

  6. WebApi和Andriod对接访问模式问题

    最近在做WebApi和Andriod接口的对接,中途出现一个问题就是返回格式的问题.由于之前使用WebService的时候使用的一直都是json的序列化和反序列话格式,所以一开始在webapi中通样使 ...

  7. Linux c- libevent

    libevent是一个事件触发的网络库,适用于windows.linux.bsd等多种平台,内部使用select.epoll.kqueue等系统调用管理事件机制.著名分布式缓存软件memcached也 ...

  8. 简单的使用POI导出excel

    package org.test; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java ...

  9. java代码-----循环变量的

    总结:输出相同的结果,很可能就是-个只是赋初始值, package com.mmm; public class Pnal { public static void main(String[] args ...

  10. 记录关于ubuntu无线上网只能ping通5~7个数据包的问题

    问题是这样的,我的笔记本(ubuntu desktop)连接上wifi后,信号很好,但是上网上不了,ping网关也不通,ping外网仅仅只有当笔记本刚刚连接上wifi的时候能ping通5至6个包,然后 ...