Talking about the Python time module and the python Module

Source: Internet
Author: User

Talking about the Python time module and the python Module

Python time module

Today, I briefly summarized the Python processing time and date modules, mainly the use of the datetime, time, and calendar modules. I hope this article will be helpful to my friends who are learning Python.

The first is the module call. Many ides have installed many modules that are frequently used by Python. Therefore, we do not need to install the modules for the time being.

1 import datetime2 import time3 import calendar

1. Get the accurate time at this time

1 # obtain the current time 2 print time. localtime ()

Output Format:

1 time.struct_time(tm_year=2015, tm_mon=12, tm_mday=29, tm_hour=1, tm_min=10, tm_sec=25, tm_wday=1, tm_yday=363, tm_isdst=0)

2. Get the date of the current day

1 # obtain the date of the current day 2 print datetime. datetime. now () 3 print datetime. date. today ()

3. Get the date of yesterday

1 # Get the date of yesterday 2 def getYesterday (): 3 today = datetime. date. today () 4 oneday = datetime. timedelta (days = 1) 5 yesterday = today-oneday 6 print type (today) # view the type of the obtained time 7 print type (yesterday) 8 return yesterday 9 yesterday = getYesterday () 10 print "Last Day:", yesterday

4. Get the date n days ago

... You don't need to provide the code. You can get the result by thinking about it.

5. String Conversion to time and date

1 # String Conversion to time 2 def strTodatetime (datestr, format): 3 return datetime. datetime. strptime (datestr, format) 4 print time. strftime ("% Y-% m-% d", time. localtime () 5 print strTodatetime ("2014-3-1", "% Y-% m-% d") 6 print time. strftime ("% Y-% m-% d % H: % M: % S", time. localtime () 7 print strTodatetime ("2005-2-16", "% Y-% m-% d")-strTodatetime ("2004-12-31 ", "% Y-% m-% d ")

Output result:

1 2015-12-292 2014-03-01 00:00:003 2015-12-29 01:10:254 47 days, 0:00:00

6. Obtain calendar-related information

1 # obtain the calendar of a month. The return string type is 2 cal = calendar. month (2015, 12) 3 print cal 4 calendar. setfirstweekday (calendar. SUNDAY) # set the first day of the calendar 5 cal = calendar. month (2015, 12) 6 print cal 7 8 # Get the calendar for one year 9 cal = calendar. calendar (2015) 10 print cal11 cal = calendar. HTMLCalendar (calendar. MONDAY) 12 print cal. formatmonth (2015, 12)

7. The calendar module can also handle the leap year problem.

1 # determine whether a leap year is used and the number of leyears between two years 2 print calendar. isleap (2012) 3 print calendar. leapdays (2010,201 5)

 

Related Article

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.