. Python datetime module built
1.datetime common class attributes, methods
Method / property name | effect | Return Type | Examples | |
Class Methods | today()/now() | Get local current time | datetime.datetime | datetime.today() ====>>>> datetime.datetime(2018, 12, 3, 10, 53, 45, 295451) |
Class name () | datetime(year,month,day,[hour,minute,second]) | Use category () directly create the object, | datetime.datetime | datetime(2018,10,2)====>>> datetime.datetime(2018, 10, 2, 0, 0) |
Examples of methods | date()/time() | Returns the date or time objects | datetime.date datetime.time |
datetime(2018,10,2)====>>> datetime.date(2018, 10, 2) |
Examples of methods | weekday() isoweekday |
Returns the day of the week, the former 0-6, 1-7 latter | int | datetime (2018,10,2) .weekday () ==== >>> 1 --- refers Tuesday |
Examples of methods | strftime (format) | According to format the datetime into str | str | datetime(2018,10,2).strftime("%Y-%m-%d") ====> '2018-10-02' |
Class Methods | the strptime (string format) | According to the str format into datetime | datetime | datetime.strptime("2018-10-2","%Y-%m-%d")====> datetime.datetime(2018, 10, 2, 0, 0) |
property property | year/month/day/hour/minute/second等 | Get information about a particular value of datetime | int | datetime(2018,10,2).year ====> 2018 |
2.timedelta common class attributes, methods
Method / property name | effect | Return Type | Examples | |
Class name () | timedelta (days, seconds, weeks) types are float |
Use category () directly create the object, | datetime.timedelta | delta h (days = 1)) ==== >>> datetime.timedelta (1) |
Examples of methods | total_seconds | Returns the total number of seconds time interval | float | timedelta(days=1).total_seconds() ====>>> 86400.0 |
property property | days,seconds, | Returns a value of a certain specific time interval | int | timedelta(days=1).days ====>>> 1 |
Second python time module built
commonly used methods:
Method name | effect | Return Type | Examples |
time() | Returns the timestamp of the current time | float | time.time() ====>>> 1543808670.1033318 |
the ctime ([Timestamp]) | The time stamp into str, have a certain format, the default incoming current timestamp | str | time.ctime() ====>>> 'Mon Dec 3 11:45:38 2018' |
localtime([时间戳] ) |
把时间戳转成time.struct_time,默认传入当前时间戳,如果要获取其中的具体某一个值,可以使用下标或者.属性tm_xxx获取 | time.struct_time 父类属于元组 里面包括(tm_year年份, tm_mon月份, tm_mday日号, tm_hour小时, tm_min分钟, tm_sec秒钟, tm_wday星期(0-6,周一=0), tm_yday今年第几天, tm_isdst=0 ) |
time.localtime() ====>>> time.struct_time(tm_year=2018, tm_mon=12, tm_mday=3, tm_hour=11, tm_min=49, tm_sec=41, tm_wday=0, tm_yday=337, tm_isdst=0) time.localtime().tm_year ====>>>2018 time.localtime()[0] ====>>>2018 |
strftime(格式,[元组或struct_time类型]) | 把元组类型或struct_time 以一定格式转化成字符串,默认传入当前时间 |
str | time.strftime("%Y-%m-%d") ====>>> '2018-12-03' |
strptime(字符串,格式) | 把时间字符串,符合这个规则的可转为struct_time类型,否则报错 | time.struct_time | ime.strptime("2018-10-02","%Y-%m-%d")====>>> time.struct_time(tm_year=2018, tm_mon=10, tm_mday=2, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=275, tm_isdst=-1) |
三. python时间格式化符号
format | 代表意义 | 区间 |
%y | 两位数的年份 | 00-99 |
%Y | 四位数的年份 | 0000-9999 |
%m | 月份 | 01-12 |
%d | 月内中的一天 | 0-31 |
%H | 24小时制小时数 | 0-23 |
%I | 12小时制小时数 | 01-12 |
%M | 分钟数 | 00-59 |
%S | 秒 | 00-59 |
%a | 本地简化星期名称 | Mon,Tues,Wed,Thur,Fri,Sat,Sun |
%A | 本地简化星期名称 | Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday |
%b | 本地简化的月份名称 | 一月Jan,二月Feb,三月Mar,四月Apr,五月May,六月June,七月July,八月Aug,九月Sept,十月Oct,十一月Nov,十二月Dec |
%B | 本地完整的月份名称 | January, February, March,April,May,June,July,August,September,October,November,December |
%w | 本地星期的数字表示 | 0-6(0代表周日) |