Python手册(Standard Library)--datetime+time+calendar

目录


datetime

datetime模块定义了6个类
datetime.date 表示日期的类
datetime.datetime 表示日期时间的类
datetime.time 表示时间的类
datetime.timedelta 表示时间间隔
datetime.tzinfo 时区的相关信息
datetime.timezone 将tzinfo抽象基类实现为UTC固定偏移量的类

datetime.timedelta类

支持数学运算

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

Attribute read-only
days Between -999999999 and 999999999 inclusive
seconds Between 0 and 86399 inclusive
microseconds Between 0 and 999999 inclusive

方法
timedelta.total_seconds()返回持续时间中包含的总秒数

datetime.date类

from datetime import date

date(year,month,day)返回 ‘year-month-day’
date.today()返回today(datetime.date类)
date.fromtimestamp(timestamp)由时间戳转化
date.fromordinal(ordinal)

timestamp(时间戳)是指格林威治时间1970年01月01日00时00分00秒起至现在的总秒数。

Attribute read-only
date.year Between MINYEAR and MAXYEAR inclusive.
date.month Between 1 and 12 inclusive.
date.day Between 1 and the number of days in the given month of the given year.

运算

date2 = date1 + timedelta
date2 = date1 - timedelta
timedelta = date1 - date2
date1 < date2   
方法 dt_date实例
dt_date.replace(year=self.year, month=self.month, day=self.day) 替换给定日期,但不改变原日期
dt_date.timetuple() 返回 time.struct_time对象(时间元祖)
dt_date.toordinal() 回归原始日期
dt_date.weekday() Return the day of the week as an integer, where Monday is 0 and Sunday is 6
dt_date.isoweekday() Return the day of the week as an integer, where Monday is 1 and Sunday is 7
dt_date.isocalendar() Return a 3-tuple, (ISO year, ISO week number, ISO weekday)
dt_date.isoformat() Return a string ‘YYYY-MM-DD’
dt_date.ctime() return a string (date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002')
dt_date.strftime(format)

datetime.datetime类

from datetime import datetime

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

创建 说明
datetime.today()
datetime.now() 返回当前系统时间
datetime.fromtimestamp(timestamp, tz=None) 根据时间戮返回datetime对象
datetime.fromordinal(ordinal)
datetime.combine(date, time, tzinfo=self.tzinfo) date对象和time对象组合成新的datetime对象
datetime.strptime(date_string, format) 字符串格式创建
Instance attributes (read-only)
datetime.year Between MINYEAR and MAXYEAR inclusive.
datetime.month Between 1 and 12 inclusive.
datetime.day Between 1 and the number of days in the given month of the given year.
datetime.hour In range(24).
datetime.minute In range(60).
datetime.second In range(60).
datetime.microsecond In range(1000000).(微秒)
datetime.tzinfo 时区
datetime.fold

运算

datetime2 = datetime1 + timedelta
datetime2 = datetime1 - timedelta
timedelta = datetime1 - datetime2
datetime1 < datetime2
方法 dt_datetime实例
dt_datetime.date() 返回date对象
dt_datetime.time() 返回time对象
dt_datetime.replace()
dt_datetime.ctime() 返回格式如 Sun Apr 16 00:00:00 2017
dt_datetime.timetuple() 返回time.struct_time对象
dt_datetime.utctimetuple()
dt_datetime.toordinal()
dt_datetime.timestamp() 返回时间戳(float)
dt_datetime.weekday() Monday is 0 and Sunday is 6
dt_datetime.isoweekday() Monday is 1 and Sunday is 7
dt_datetime.isocalendar() Return a 3-tuple, (ISO year, ISO week number, ISO weekday)
dt_datetime.isoformat(sep=’T’, timespec=’auto’) Return a string ‘YYYY-MM-DDTHH:MM:SS.mm’
dt_datetime.ctime() return a string (‘Wed Dec 4 20:30:40 2002’)
dt_datetime.strftime(format) 由日期格式转化为字符串格式

关于时区的方法暂时不计入

datetime.time类

from datetime import time

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

Instance attributes (read-only)
time.hour In range(24).
time.minute In range(60).
time.second In range(60).
time.microsecond In range(1000000).
time.tzinfo
time.fold
方法
dt_time.replace(hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, * fold=0)
dt_time.isoformat(timespec=’auto’)
dt_time.strftime(format) 转字符格式
dt_time.tzname() 返回时区名字
dt_time.utcoffset() 返回时区的时间偏移量
dt_time.dst()

python中时间日期格式化符号

格式 说明 Example
%a 周日期缩写 Sun, Mon, …, Sat (en_US);
%A 周日期全称 Sunday, Monday, …, Saturday (en_US);
%w 周数字 0, 1, …, 6
%d 月中天数 01, 02, …, 31
%b 月份缩写 Jan, Feb, …, Dec (en_US);
%B 月份全称 January, February, …, December (en_US);
%m 月份数字 01, 02, …, 12
%y 年数字,两位 00, 01, …, 99
%Y 年数字,四位 0001, 0002, …, 2013, 2014, …, 9998, 9999
%H 24小时制 00, 01, …, 23
%I 12小时制 01, 02, …, 12
%p AM or PM AM, PM (en_US);
%M 分钟 00, 01, …, 59
%S 00, 01, …, 59
%f 微秒 000000, 000001, …, 999999
%z UTC offset in the form +HHMM or -HHMM (empty), +0000, -0400, +1030
%Z 时区名 (empty), UTC, EST, CST
%j 年中的天数 001, 002, …, 366
%U 年中的周日期(周日为第一天) 00, 01, …, 53
%W 年中的周日期(周一为第一天) 00, 01, …, 53
%c date and time Tue Aug 16 21:30:00 1988 (en_US);
%x date 08/16/88 (None)
08/16/1988 (en_US)
%X time 21:30:00 (en_US);
%% %’字符 %
%G ISO 8601 year 0001, 0002, …, 2013, 2014, …, 9998, 9999
%u ISO 8601 weekday 1, 2, …, 7
%V ISO 8601 week 01, 02, …, 53

time

函数
time.clock() 用CPU花费的时间(秒)
time.sleep(secs)

定义时间戳类
time.struct_time

属性
tm_year yyyy
tm_mon 1 到 12
tm_mday 1 到 31
tm_hour 0 到 23
tm_min 0 到 59
tm_sec 0 到 61 (60或61 是闰秒)
tm_wday 0到6 (0是周一)
tm_yday 1 到 366
tm_isdst 1(夏令时)0(不是夏令时)-1(未知)

calendar

星期一是默认的每周第一天,星期天是默认的最后一天。更改设置需调用calendar.setfirstweekday()函数。

calendar函数
calendar.calendar(year,w=2,l=1,c=6) 年日历
calendar.firstweekday() 返回当前每周起始日期的设置
calendar.month(year,month) 月日历
calendar.isleap(year) 是否闰年
calendar.leapdays(y1,y2) 返回在Y1,Y2两年之间的闰年总数
calendar.monthrange(year, month) Returns weekday of first day of the month and number of days in month
calendar.weekday(year,month,day) Returns the day of the week (0 is Monday)

猜你喜欢

转载自blog.csdn.net/qq_41518277/article/details/80261023
今日推荐