Python small white-day6 time&datetime module

Source: Internet
Author: User
Tags timedelta

Time&datetime

? One, Time module

Time module provides functions for various operating times

< Span style= "color: #333333; Font-family:arial;font-size:14px;line-height:26px;widows:1;background-color: #FFFFFF;" > Description: There are generally two ways to represent time:

123456789101112 import timeprint(‘clock():‘,time.clock()) #返回处理器时间,3.3开始已废弃print(‘process_time():‘,time.process_time()) #返回处理器时间,3.3开始已废弃print(‘time():‘,time.time()) #返回当前系统时间戳print(‘ctime():‘,time.ctime()) #输出当前系统时间print(‘ctime(time.time()-86640):‘,time.ctime(time.time()-86640)) #将时间戳转为字符串格式print(‘gmtime(time.time()-86640):‘,time.gmtime(time.time()-86640)) #将时间戳转换成struct_time格式print(‘localtime(time.time()-86640):‘,time.localtime(time.time()-86640)) #将时间戳转换成struct_time格式,但返回 的本地时间print(time.mktime(time.localtime())) #与time.localtime()功能相反,将struct_time格式转回成时间戳格式#time.sleep(4) #sleepprint(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将struct_time格式转成指定的字符串格式print(time.strptime("2016-02-03","%Y-%m-%d") ) #将字符串格式转换成struct_time格式


Second, the DateTime module

1234567891011121314151617181920 import timeimport datetimeprint(datetime.date.today()) #输出格式 2016-02-03print(datetime.date.fromtimestamp(time.time()-864400) ) #2016-02-03 将时间戳转成日期格式current_time = datetime.datetime.now() #print(current_time) #输出2016-02-03 20:27:37.188100print(current_time.timetuple()) #返回struct_time格式 #datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])print(current_time.replace(2014,9,12)) #输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换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)new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天print(new_date)new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时print(new_date)new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120sprint(new_date)




From for notes (Wiz)

Python small white-day6 time&datetime module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.