import time print(time.clock())print(time.process_time())print(time.time()) #返回当前系统时间戳print(time.ctime()) #返回当前系统时间print(time.ctime(time.time()-86640)) #将时间戳转为字符串print(time.gmtime(time.time()-86640)) #将时间戳转为struct_time格式print(time.localtime(time.time()-86640)) #将时间戳转为struct_time格式,但返回本地赶时间print(time.mktime(time.localtime())) #与time.localtime()功能相反,将struct_time转回时间戳#time.sleep(1) #睡眠4秒print(time.strftime("%Y-%m-%d %H:%M:%S"),time.gmtime())#将struct_time转成指定字符串格式print(time.strptime("2016-01-28","%Y-%m-%d")) #字符串格式转成struct_time import datetime print(datetime.date.today()) #输出格式 2016-05-25print(datetime.date.fromtimestamp(time.time() - 864400)) #将时间戳转成日期格式current_time = datetime.datetime.now()print(current_time) #输出2016-05-25 19:33:49.521486print(current_time.timetuple()) #返回struct_time格式print(current_time.replace(2014,9,12)) #输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换str_to_date = datetime.datetime.strptime("2016-05-25 19:42:00","%Y-%m-%d %H:%M:%S") #将字符串转换成日期格式new_date = datetime.datetime.now() + datetime.timedelta(days=10)new_date = datetime.datetime.now() + datetime.timedelta(days=-10)new_date = datetime.datetime.now() + datetime.timedelta(hours=-10)new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) 时间参数及其函义:
参数 |
函义 |
%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. |
%d |
Day of the month as a decimal number [01,31]. |
%H |
Hour (24-hour clock) as a decimal number [00,23]. |
%I |
Hour (12-hour clock) as a decimal number [01,12]. |
%j |
Day of the year as a decimal number [001,366]. |
%m |
Month as a decimal number [01,12]. |
%M |
Minute as a decimal number [00,59]. |
%p |
Locale’s equivalent of either AM or PM. |
%S |
Second as a decimal number [00,61]. |
%U |
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. |
%w |
Weekday as a decimal number [0(Sunday),6]. |
%W |
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. |
%x |
Locale’s appropriate date representation. |
%X |
Locale’s appropriate time representation. |
%y |
Year without century as a decimal number [00,99]. |
%Y |
Year with century as a decimal number. |
%z |
Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. |
%Z |
Time zone name (no characters if no time zone exists). |
%% |
A literal ‘%‘ character. |
时间: 2024-10-01 02:29:35