Python Timing Module Time & datetime & Calendar Introduction

Source: Internet
Author: User
Tags month name

There are two modules for Python processing time, which are times, datetime, calendar

1> time

Print(Time.gmtime ())Print(Time.localtime ())#the current time is returned by a Time.struct_time objectPrint(Time.time ())#The time stamp of the current time is returnedLocalTime =time.localtime ()Print("tm_gmtoff={}". Format (localtime.tm_gmtoff))Print("tm_hour={}". Format (Localtime.tm_hour))#whenPrint("tm_isdst={}". Format (LOCALTIME.TM_ISDST))#whether daylight saving timePrint("tm_mday={}". Format (Localtime.tm_mday))#RIPrint("tm_min={}". Format (localtime.tm_min))#pointsPrint("tm_mon={}". Format (Localtime.tm_mon))#MonthPrint("tm_sec={}". Format (LOCALTIME.TM_SEC))#secondsPrint("tm_wday={}". Format (Localtime.tm_wday))#weeksPrint("tm_yday={}". Format (Localtime.tm_yday))#the day ordinal of a yearPrint("tm_year={}". Format (localtime.tm_year))#yearsPrint("tm_zone={}". Format (localtime.tm_zone)) TS= Time.mktime (localtime)#get time stamps at a given timePrint(TS)#Format output TimePrint(Time.strftime ("%y/%m/%d", localtime))#the time string is returnedTimestr="2017-08-05 23:00:00"Print(Time.strptime (TIMESTR,"%y-%m-%d%x"))#a Time.struct_time object is returned.

2> datetime

#in the DateTime module, there are several sub-modules, respectively:#Date: Processing date#time: Minutes and minutes of processing, subtle#datetime: Processing date, hour, minute, subtle#see the properties and methods of each module separatelyPrint('----------------The date module--------------------')Print(dir (datetime.date))Print('----------------Time Module--------------------')Print(dir (datetime.time))Print('----------------a datetime module----------------')Print(dir (datetime.datetime))#here is the main look at the datetime module fromDatetimeImportdatetimePrint(DateTime. Minyear,datetime. Maxyear)#maximum year and minimum year, you can make your own default said 9999, 1Datetime. Maxyear = 100000Print(DateTime. Maxyear)#to view the types that datetime belongs toPrint(Type (datetime))#discovery is a type#Here's a record of the relationship between type and Object#Object is the top of all parent-child relationships (inheritance relationships), all of which are the parent of the data type, and type is all type instance relationships#At the top, all objects are instances of it#As you can understand, object is a type that is an instance of the type Object#The above understanding is only a personal understanding, if there is a wrong place, please correct#Get current TimeCurrentTime = DateTime.Now ()#a Datetime.datetime object is returned.Print(currenttime)Print(Currenttime.strftime ('%x%x'))#format the current time of the output#gets the timestamp of the current timePrint(Currenttime.timestamp ()) d=datetime (2017,8,6,11,52,30)#custom format output for a specified time#%y Two-digit year representation (00-99)#%Y Four-digit year representation (000-9999)#%m Month (01-12)#One day in%d months (0-31)#%H 24-hour system hours (0-23)#%I 12-hour hours (01-12)#%M minutes (00=59)#%s seconds (00-59)#%a Local Simplified week name#%A Local Full week name#%b a locally simplified month name#%B Local Full month name#%c Local corresponding date representation and time representation#%j Day of the Year (001-366)#%p the equivalent of a local a.m. or P.M.#%u weeks of the year (00-53) Sunday is the beginning of the week#%w Week (0-6), Sunday for the beginning of the week#%W Week of the Year (00-53) Monday is the beginning of the week#%x Local corresponding date representation#%x Local corresponding time representation#%Z The name of the current time zone#percent% of the number itself     Print(D.strftime ("%y-%m-%d%p"))#2017-08-06 AM A string#Show English formatPrint(D.ctime ())#Sun 6 11:52:30#Show Calendar (year, Week of the year, weeks)Print(D.isocalendar ())#(2017, 31, 7) returns a tuple that needs to be directed to a value when usedPrint(Datetime.today ())

3> Calendar

#get a calendar for a yearPrint(Calendar.calendar (2017))#get a calendar for a monthPrint(Calendar.month (2017, 8))#determine whether a year is a leap yearsPrint(Calendar.isleap (2017))#detects the number of leap years between two year (S )Print(Calendar.leapdays (1990,2017))#Convert calendar to HTML format, you can set date, week, etc.Cal =Calendar. Htmlcalendar (Calendar. MONDAY)Print(CAL)Print(Cal.formatyear (2017))

Python Timing Module Time & datetime & Calendar Introduction

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.