This article briefly summarizes the Python processing time and date aspects of the module, mainly the use of datetime, three modules, I hope this article is useful for learning Python friends.
The first is the call of the module, many Ides have installed a lot of Python frequently used modules, so we do not need to install the module.
Import Datetimeimport Timeimport Calendar
1. Get the exact time at this point
# Get the time at this moment print time.localtime ()
The output format is: 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 day
# Get date of day print Datetime.datetime.now () print datetime.date.today ()
3. Get yesterday's date
# Get yesterday's date def getyesterday (): Today = Datetime.date.today () oneday = Datetime.timedelta (Days=1) yesterday = Today-oneday Print type (today) # View Gets the type of time print type (yesterday) return yesterdayyesterday = Getyesterday () print "Yesterday's Time:", Yesterday
4. Get the date n days before
This should not be given the code, a little to think about the results can be obtained.
5. String conversion to time and date
# string converted to time Def strtodatetime (DATESTR, format): Return Datetime.datetime.strptime (DATESTR, format) print Time.strftime (" %y-%m-%d ", Time.localtime ()) Print strtodatetime (" 2014-3-1 ","%y-%m-%d ") Print Time.strftime ("%y-%m-%d%h:%m:%s ", Time.localtime ()) Print strtodatetime ("2005-2-16", "%y-%m-%d")-strtodatetime ("2004-12-31", "%y-%m-%d")
Output Result:
2015-12-29
2014-03-01 00:00:00
2015-12-29 01:10:25
Days, 0:00:00
6. Get calendar-related information
# Gets the calendar for a month, returns the string type Cal = Calendar.month (+), print calcalendar.setfirstweekday (calendar. SUNDAY) # Set the first day of the Calendar cal = Calendar.month ($) Print cal# get calendar cal for one year = Calendar.calendar print calcal = Calendar. Htmlcalendar (Calendar. MONDAY) Print Cal.formatmonth (2015, 12)
The 7.calendar module can also handle leap year issues
# Determine if a leap year, the number of leap years between two calendar.isleap print Calendar.leapdays (2010, 2015)
The Python time module datetime\time is discussed in detail.
The corresponding meaning of the escape character is as follows
- %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
- One day in%d months (0-31)
- %H 24-hour hours (0-23)
- %I 12-hour hours (01-12)
- %j Day of the Year (001-366)
- %m Month (01-12)
- %M minutes (00=59)
- %p the equivalent of a local a.m. or p.m.
- %s seconds (00-59)
- %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
- %y Two-digit year representation (00-99)
- %Y Four-digit year representation (000-9999)
- %Z the name of the current time zone
- Percent% of the number itself
Code:
Import time Import datetime #两日期相减 D1 = Datetime.datetime (2005, 2, +) D2 = Datetime.datetime (2004, +) print (D1- D2). Days #运行时间: StartTime = Datetime.datetime.now () Endtime = Datetime.datetime.now () print (endtime-starttime). Seco NDS #计算当前时间向后10天的时间. # If it is hours days change to hours D1 = Datetime.datetime.now () d3 = D1 Datetime.timedelta (day =10) print str (d3) print d3.ctime () t Ime.ctime ([sec]) #把秒数转换成日期格式, if no parameters are present, displays the current time. >>> import time>>> time.ctime () >>> "Wed June 15:02:50 2006" >>> Time.ctime ( 1138068452427683) "Sat Dec 04:51:44 1901" >>> Import time>>> time.strftime ("%y-%m-%d%x", Time.localtime ()) "2011-03-15 20:42:12" >>> time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()) "2011-03-15 20:03:47 "datetime module----------------------------datetime convert date to seconds >>> import datetime,time>>> Time.mktime (Datetime.datetime (2009,1,1) timetuple ()) 1230739200.0>>> cc=[2000,11,3,12,43,33] #Attributes : Year, MontH, day, hour, minute, second>>> Time.mktime (Datetime.datetime (cc[0],cc[1],cc[2],cc[3],cc[4],cc[5]). Timetuple ()) 973226613.0time.time () get the current time; Time.localtime () Get local time; Time.strftime () format date; Time.strptime (timestring) Convert the string to a date; the date of the input is the day of the week >>> Datetime.datetime (2011,02,15). Weekday () 1>>> Datetime.datetime ( 2011,02,15). Weekday () 1>>> datetime.datetime (2011,02,16). Weekday () 2>>> Datetime.datetime ( 2011,02,17). Weekday () 3>>>datetime module get current time >>> Datetime.datetime.utcnow () datetime.datetime (2011 , 3, 264194) >>> Datetime.datetime.utcnow (). Strftime ("%y-%m-%d%h:%m:%s") format ' 2011-03-15 13:19:27 ' >>>
The above is about the Python time module of the detailed study, I hope that you learn Python program design is helpful.