开篇之前先留下我的疑问,留给以后的自己解答吧。为什么有datetime这个类,Python中还要date和time类干嘛。这不是多次一举吗?不是要提升开发人员的效率吗,让开发人员记那么多类似的东西,有利于开发吗?
该构造函数,包含了date与time的构造函数。year, month, day三个参数是必填的,hour, minute, second, microsecond 是选填的,并且默认值为0,tzinfo 是选填的,默认值为None
参数值的范围如下:
MINYEAR <= year <= MAXYEAR
1 <= month <= 12
1 <= day <= number of days in the given month and year
0 <= hour < 24
0 <= minute < 60
0 <= second < 60
0 <= microsecond < 1000000
如果 超出范围 则抛出ValueError的异常。
二、属性
datetime.min==datetime(MINYEAR, 1, 1, tzinfo=None)==0001-01-01 00:00:00
datetime.max == datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, tzinfo=None) ==9999-12-31 23:59:59.999999
datetime.resolution == timedelta(microseconds=1) == 0:00:00.000001;
三、相关方法:
静态方法:
1.datetime.today()
返回今天的时间
2.datetime.now(tz=None)
tz表示时区,返回指定时区的时间
3. datetime.utcnow()
返回utc时间
4.datetime.fromtimestamp(timestamp, tz=None)
根据时间戳返回指定时区的时间
5.datetime.utcfromtimestamp(timestamp)
根据时间戳返回utc时区的时间
6.datetime.fromordinal(ordinal)
根据天数+最小时间,返回时间
7.datetime.combine(date, time)
结合日期与时间,获取详细 的时间
8.datetime.strptime(date_string, format)
返回指定格式的时间
实例方法:
1.datetime.date()
返回date对象
2.datetime.time()
返回time对象,但是tzinfo为None
3.datetime.timetz()
返回time对象,所有的属性都相同
3.datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])
返回替换后的时间,必须保证上一级的参数有值。
4.datetime.astimezone(tz=None)
返回一个新时区的时间,tzinfo为时区
5.datetime.toordinal()
返回距离最小时间的天数
6.datetime.timestamp()
返回时间戳
7.datetime.weekday()
返回当前日期是所在周的第几天 0 表示周一 6 表示周日
8.datetime.isoweekday()
返回当前日期是所在周的第几天 1 表示周一 7 表示周日
9.datetime.isocalendar()
返回一个时间的元组
10.datetime.isoformat(sep='T')
返回指定格式的时间 YYYY-MM-DDTHH:MM:SS.mmmmmm 等价 datetime.__str__()
11.datetime.ctime()
返回特定格式的时间 datetime(2002, 12, 4, 20, 30, 40).ctime() == 'Wed Dec 4 20:30:40 2002'
12.datetime.strftime(format)
返回指定时区的时间 等价 datetime.__format__(format)
一、定义
该构造函数,包含了date与time的构造函数。year, month, day三个参数是必填的,hour, minute, second, microsecond 是选填的,并且默认值为0,tzinfo 是选填的,默认值为None
参数值的范围如下:
MINYEAR <= year <= MAXYEAR
1 <= month <= 12
1 <= day <= number of days in the given month and year
0 <= hour < 24
0 <= minute < 60
0 <= second < 60
0 <= microsecond < 1000000
如果 超出范围 则抛出ValueError的异常。
二、属性
datetime.min==datetime(MINYEAR, 1, 1, tzinfo=None)==0001-01-01 00:00:00
datetime.max == datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, tzinfo=None) ==9999-12-31 23:59:59.999999
datetime.resolution == timedelta(microseconds=1) == 0:00:00.000001;
三、相关方法:
静态方法:
1.datetime.today()
返回今天的时间
2.datetime.now(tz=None)
tz表示时区,返回指定时区的时间
3. datetime.utcnow()
返回utc时间
4.datetime.fromtimestamp(timestamp, tz=None)
根据时间戳返回指定时区的时间
5.datetime.utcfromtimestamp(timestamp)
根据时间戳返回utc时区的时间
6.datetime.fromordinal(ordinal)
根据天数+最小时间,返回时间
7.datetime.combine(date, time)
结合日期与时间,获取详细 的时间
8.datetime.strptime(date_string, format)
返回指定格式的时间
实例方法:
1.datetime.date()
返回date对象
2.datetime.time()
返回time对象,但是tzinfo为None
3.datetime.timetz()
返回time对象,所有的属性都相同
3.datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])
返回替换后的时间,必须保证上一级的参数有值。
4.datetime.astimezone(tz=None)
返回一个新时区的时间,tzinfo为时区
5.datetime.toordinal()
返回距离最小时间的天数
6.datetime.timestamp()
返回时间戳
7.datetime.weekday()
返回当前日期是所在周的第几天 0 表示周一 6 表示周日
8.datetime.isoweekday()
返回当前日期是所在周的第几天 1 表示周一 7 表示周日
9.datetime.isocalendar()
返回一个时间的元组
10.datetime.isoformat(sep='T')
返回指定格式的时间 YYYY-MM-DDTHH:MM:SS.mmmmmm 等价 datetime.__str__()
11.datetime.ctime()
返回特定格式的时间 datetime(2002, 12, 4, 20, 30, 40).ctime() == 'Wed Dec 4 20:30:40 2002'
12.datetime.strftime(format)
返回指定时区的时间 等价 datetime.__format__(format)
四、实例
from datetime import datetime,date,timedelta,time
d= datetime(2017,1,5);
print(datetime.today());
print(datetime.now());
print(datetime.utcnow());
print(datetime.combine(date(2017,1,5),time()));#2017-01-05 00:00:00
print(d.date());#2017-01-05
print(d.time());#00:00:00
print(d.timetz());#0:00:00
print(d.replace(2016,1,5,12,25,13));#2016-01-05 12:25:13
print(d.toordinal());#736334
print(d.weekday());#3
print(d.isoweekday());#4
print(d.isocalendar());#(2017, 1, 4)
print(d.isocalendar()[0])#表示年份
print(d.isocalendar()[1])#表示一年内的第几个周
print(d.isocalendar()[2])#表示周几
print(d.isoformat());#2017-01-05T00:00:00
print(d.ctime());#Thu Jan 5 00:00:00 2017
print(d.__format__("%d/%m/%y %H:%M"));#05/01/17 00:00
print(d.strftime("%d/%m/%y %H:%M"));#05/01/17 00:00
print(datetime.min);#0001-01-01 00:00:00
print(datetime.max);#9999-12-31 23:59:59.999999
print(datetime.resolution);#0:00:00.000001
以上为自己翻译的文档,若有错误,麻烦指正。谢谢!
出处: