Python模块之time&datetime

time&datetime模块

  Python提供了多个内置模块用于操作日期时间,例如:calendar,time,datetime。time模块它提供的接口与C标准库time.h基本一致。相比于time模块,datetime模块的接口则更直观、更容易调用。

>>> import time
>>> import datetime
>>> print(time.clock())         # 返回处理器时间,3.3开始已废弃 2.199715466804369e-06 >>> print(time.process_time())     # 返回处理器时间,3.3开始已废弃 6.9888448 >>> print(time.time())          # 返回当前系统时间戳 1465222058.1238248 >>> print(time.ctime())         # 输出Tue Jan 26 18:23:48 2016 ,当前系统时间 Mon Jun 6 22:07:38 2016 >>> print(time.ctime(time.time()-86640)) # 将时间戳转为字符串格式 Sun Jun 5 22:03:38 2016 >>> print(time.gmtime(time.time()-86640)) # 将时间戳转换成struct_time格式 time.struct_time(tm_year=2016, tm_mon=6, tm_mday=5, tm_hour=14, tm_min=3, tm_sec=38, tm_wday=6, tm_yday=157, tm_isdst=0) >>> print(time.localtime(time.time()-86640)) # 将时间戳转换成struct_time格式,但返回的本地时间 time.struct_time(tm_year=2016, tm_mon=6, tm_mday=5, tm_hour=22, tm_min=3, tm_sec=38, tm_wday=6, tm_yday=157, tm_isdst=0) >>> print(time.mktime(time.localtime())) # 与time.localtime()功能相反,将struct_time格式转回成时间戳格式 1465222059.0

#time.sleep(4) #sleep >>> print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) # 将struct_time格式转成指定的字符串格式 2016-06-06 14:07:39 >>> print(time.strptime("2016-01-28","%Y-%m-%d") )       # 将字符串格式转换成struct_time格式 time.struct_time(tm_year=2016, tm_mon=1, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=28, tm_isdst=-1) #datetime module >>>print(datetime.date.today())   # 输出格式 2016-01-26 2016-06-06 >>> print(datetime.date.fromtimestamp(time.time()-864400) ) #2016-01-16 将时间戳转成日期格式 2016-05-27 >>> current_time = datetime.datetime.now() >>> print(current_time)          # 输出2016-01-26 19:04:30.335935 2016-06-06 22:07:40.183942 >>> print(current_time.timetuple())   # 返回struct_time格式 time.struct_time(tm_year=2016, tm_mon=6, tm_mday=6, tm_hour=22, tm_min=7, tm_sec=40, tm_wday=0, tm_yday=158, tm_isdst=-1) # datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]]) >>>print(current_time.replace(2014,9,12))     # 输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换 2014-09-12 22:07:40.183942 >>>str_to_date = datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")   # 将字符串转换成日期格式 >>> new_date = datetime.datetime.now() + datetime.timedelta(days=10)          # 比现在加10天 >>> print(new_date) 2016-06-16 22:11:14.076176 >>> new_date = datetime.datetime.now() + datetime.timedelta(days=-10) # 比现在减10天 >>> print(new_date) 2016-05-27 22:11:14.339191 >>> new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) # 比现在减10小时 >>> print(new_date) 2016-06-06 12:11:14.608207 >>> new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) # 比现在+120s >>> print(new_date) 2016-06-06 22:13:14.880222

%Y  Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
posted @ 2016-08-01 11:01  每天进步一点点!!!  阅读(128)  评论(0编辑  收藏  举报