首页 » 技术分享 » datetime.datetime类介绍

datetime.datetime类介绍

 

datetime类是date和time的结合体,包括date与time的所有信息,date和time类中具有的方法和属性,datetime类都具有。

所以在我们日常的工作中,可以仅使用datetime类。该类的构造函数: 
datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]]) 
各参数的含义与date、time构造函数中的一样,但是要注意各参数的取值范围。 

1、today函数  返回一个当前本地时间的datetime.datetime类的对象。
2、now([tz]) 不指定时区,返回一个当前本地时间的datetime.datetime类的对象。指定时区,返回指定时区的时间
3、fromtimestamp(timestamp[,tz]) 给定一个时间戳,返回指定时区的datetime.datetime类的对象。不指定时区,返回本地时区的datetime类对象
4、strptime("时间字符串",format) 将格式时间字符串转换为datetime对象
5、Datetime类—year、month、day、hour、minute、second属性 

>>> import datetime

>>> datetime.datetime.today()

datetime.datetime(2018, 1, 17, 21, 8, 41, 964000)

>>> print datetime.datetime.today()

2018-01-17 21:08:55.260000

>>> print datetime.date.today()

2018-01-17

>>> print datetime.datetime.now()

2018-01-17 21:09:15.708000

>>> import time

>>> print datetime.datetime.fromtimestamp(time.time())

2018-01-17 21:10:22.618000

>>> time.now()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'module' object has no attribute 'now'

>>> print datetime.datetime.strptime("2018-01-16 23:44:55","%Y-%m-%d %H:%M:%S")

2018-01-16 23:44:55

>>> datetime.datetime.strptime("2018-01-16 23:44:55","%Y-%m-%d %H:%M:%S")

datetime.datetime(2018, 1, 16, 23, 44, 55)

>>> tm3=datetime.datetime.strptime("2018-01-16 23:44:55","%Y-%m-%d %H:%M:%S")

>>> tm3.year

2018

>>> tm3.day

16

>>> tm3.month

1

>>> tm3.hour

23

>>> tm3.minute

44

>>> tm3.second

55

>>> tm3

datetime.datetime(2018, 1, 16, 23, 44, 55)

>>> tm3.microsecond

0

>>>


6、datetime.date() 参数是datetime.datetime类的对象,得到一个datetime.date对象
7、datetime.time() 参数是datetime.datetime类的对象,得到一个datetime.time对象
8、datetime.combime() 参数是datetime.datetime类的对象、datetime.date对象、datetime.time对象,得到一个datetime.datetime对象

>>> datetime.datetime.date(datetime.datetime.today())

datetime.date(2018, 1, 17)

>>> datetime.datetime.time(datetime.datetime.today())

datetime.time(21, 25, 16, 365000)

>>> datetime.datetime.combine(datetime.datetime.today(),datetime.time(10,34,55))

datetime.datetime(2018, 1, 17, 10, 34, 55)

>>> datetime.datetime.combine(datetime.date(2016,4,6),datetime.time(10,10,10))

datetime.datetime(2016, 4, 6, 10, 10, 10)

>>>

9、datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]]) 函数

>>> tm4=datetime.datetime(2016, 4, 6, 10, 10, 10)

>>> tm4

datetime.datetime(2016, 4, 6, 10, 10, 10)

>>> tm5= tm4.replace(year=2018)

>>> tm5

datetime.datetime(2018, 4, 6, 10, 10, 10)

>>> tm4

datetime.datetime(2016, 4, 6, 10, 10, 10)

>>>

10、datetime.timetuple()函数:得到一个time.struct_time对象

>>> tm4

datetime.datetime(2016, 4, 6, 10, 10, 10)

>>> tm4.timetuple()

time.struct_time(tm_year=2016, tm_mon=4, tm_mday=6, tm_hour=10, tm_min=10, tm_se

c=10, tm_wday=2, tm_yday=97, tm_isdst=-1)

>>> tm5=tm4.timetuple()

>>> print tm5

time.struct_time(tm_year=2016, tm_mon=4, tm_mday=6, tm_hour=10, tm_min=10, tm_se

c=10, tm_wday=2, tm_yday=97, tm_isdst=-1)

>>> time.mktime(tm5)

1459908610.0

>>>

11、datetime.weekday(time)函数  参数time是一个datetime.date类型或datetime.datetime类型的数据。返回weekday中的星期几,星期一,返回0;星期二,返回1;以此类推。 

以下可以看出,参数只能是datetime.datetime类的对象或datetime.date类的对象



>>> tm5

time.struct_time(tm_year=2016, tm_mon=4, tm_mday=6, tm_hour=10, tm_min=10, tm_se

c=10, tm_wday=2, tm_yday=97, tm_isdst=-1)

>>> tm4

datetime.datetime(2016, 4, 6, 10, 10, 10)

>>> tm3=datetime.date.today()

>>> tm3

datetime.date(2018, 1, 17)

>>> datetime.datetime.weekday(tm5)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: descriptor 'weekday' requires a 'datetime.date' object but received a

 'time.struct_time'

>>> datetime.datetime.weekday(tm3)

2

>>> datetime.datetime.weekday(tm4)

2

>>>

12、datetime.isocalendar(time)函数  参数time是一个datetime.date类型或datetime.datetime类型的数据。返回元组(year,month,day)

>>> tm5

time.struct_time(tm_year=2016, tm_mon=4, tm_mday=6, tm_hour=10, tm_min=10, tm_se

c=10, tm_wday=2, tm_yday=97, tm_isdst=-1)

>>> tm4

datetime.datetime(2016, 4, 6, 10, 10, 10)

>>> tm3=datetime.date.today()

>>> tm3

datetime.date(2018, 1, 17)

>>> datetime.datetime.isocalendar(tm5)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: descriptor 'isocalendar' requires a 'datetime.date' object but receiv

ed a 'time.struct_time'

>>> datetime.datetime.isocalendar(tm4)

(2016, 14, 3)

>>> datetime.datetime.isocalendar(tm3)

(2018, 3, 3)

>>> type(datetime.datetime.isocalendar(tm3))

<type 'tuple'>

>>>

13、datetime.strftime(format)函数  返回自定义的格式时间字符串。参数format是自定义的时间格式。 

>>> tm4

datetime.datetime(2016, 4, 6, 10, 10, 10)

>>> tm4.strftime("%Y-%m-%d %H:%M:%S")

'2016-04-06 10:10:10'

>>>

转载自原文链接, 如需删除请联系管理员。

原文链接:datetime.datetime类介绍,转载请注明来源!

0