The example in this paper describes the date operation of Python programming development. Share to everyone for your reference, as follows:
The libraries that manipulate dates in Python are:
Import datetime
Import time
For date formatting information, you can refer to the official API:
Time.strftime
Datetime
Here is the demo I made:
#datetimeimport datetime# Current Date now = Datetime.datetime.now () print (Now.strftime ('%y-%m-%d%h:%m:%s ')) print ( Now.strftime ('%y-%m-%d ')) #string convert to Datetimetime_str = ' 2013-07-29 01:05:00 ' str_convert_2_time = Datetime.datetime.strptime (Time_str, '%y-%m-%d%h:%m:%s ') print (str_convert_2_time) #比较两个日期相差多少天time_strA = ' 2013-07-29 01:05:00 ' time_strb = ' 2013-08-29 01:05:00 ' day = Datetime.datetime.strptime (Time_stra, '%y-%m-%d%h:%m:%s ') Day2 = Datetime.datetime.strptime (TIME_STRB, '%y-%m-%d%h:%m:%s ') Sub_day = Day2-dayprint (' {0} and {1} differ {2} days '. Format (time _stra, TIME_STRB, str (sub_day.days))) #今后的n天的日期n_days = 4now = Datetime.datetime.now () my_date = Datetime.timedelta (days =n_days) N_day = now + my_dateprint (' the date of {0} days from today is: '. Format (n_days)) print (N_day.strftime ('%y-%m-%d%h:%m:%s ')
Operating effect:
Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 + bit (Intel)] on Win32type "copyright", "credits" or "license ()" For more information.>>> ================================ RESTART =========================== =====>>> 2013-07-29 01:48:162013-07-292013-07-29 01:05:002013-07-29 01:05:00 and 2013-08-29 01:05:00 difference 31 days 4 days from today's date is: 2013-08-02 01:48:16>>>
I hope this article is helpful for Python program design.