首页 > 代码库 > time&datetime&random模块

time&datetime&random模块

import time
1、print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
2、print(time.altzone)  #返回与utc时间的时间差,以秒计算3、print(time.asctime()) #返回时间格式"Thu Apr 13 21:46:21 2017",
4、print(time.localtime()) #返回本地时间 的struct time对象格式
5、print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式
6、print(time.asctime(time.localtime())) #返回时间格式"Thu Apr 13 21:46:21 2017", 7、print(time.ctime()) #返回Thu Apr 13 21:46:21 2017 格式, 同上 8、日期字符串 转成 时间戳 9、string_2_struct = time.strptime("2017/04/13","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式 10、print(string_2_struct) 11、struct_2_stamp = time.mktime(string_2_struct) #将struct时间对象转成时间戳 12、print(struct_2_stamp) #将时间戳转为字符串格式 13、print(time.gmtime(time.time()-86640)) #将utc时间戳转换成struct_time格式 14、print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将utc struct_time格式转成指定的字符串格式 #时间加减 import datetime 1、print(datetime.datetime.now()) #返回 2017-08-19 12:47:03.941925 2、print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19 3、print(datetime.datetime.now() ) 4、print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天 5、print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天 6、print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时 7、print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分 8、c_time = datetime.datetime.now() 9、print(c_time.replace(minute=3,hour=2)) #时间替换

DirectiveMeaningNotes
%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. (1)
%S Second as a decimal number [00,61]. (2)
%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. (3)
%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. (3)
%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.


####random模块


import random
print random.random()#产生随机数
print random.randint(1,10)#1-10之间的随机整数
print random.randrange(1,10)#1-10之间不包括(1和10)的随机整数
 
写个随机生成验证码的函数玩玩
##随机生成验证码
def get_code():
checkcode = ‘‘#初始化验证码
strings=[] #验证码生成来源
for i in range(1,5): #生成四个验证码
current = random.randrange(0,5)
if current != i:
chr1 = chr(random.randint(65,90)) #将数字转换成大写字母
chr2= chr(random.randint(97,122)) #将数字转换成小写字母
strings.append(chr1)
strings.append(chr2)
else:
digit = random.randint(0,9)
strings.append(str(digit))
checkcode += random.choice(strings)+" "
print(checkcode)
if __name__=="__main__":
get_code()
 

time&datetime&random模块