DateTime encapsulates a number of methods on a time basis. But time is often used, the function in datetime, time can achieve one, datetime of three modules Datetime.datedatetime.timedatetime.datetime mainly use this module import Datetime1, Datetime.datetime.now () Gets the current time, DateTime format T1=datetime.datetime.now () print T1print T1.yearprint T1.day2017-05-28 11:32:21.853000 (subtle: 853000) 201728 2, fromtimestamp timestamp converted to Datetimet2=time.time () dt= Datetime.datetime.fromtimestamp (T2) Print Dtprint type (dt) Result: 2017-05-28 11:34:20.725000<type ' Datetime.datetime ' > 3, strftime () formatted output print Datetime.datetime.now (). Strftime ('%y-%m-%d%h:%m:%s ') 4, replace () Replace a value in datetime with the desired value T1=datetime.datetime.now () t2=t1.replace (month=4,hour=23) Print T2 result: 2017-04-28 23:50:10.855000t3=t1-t2print type (t3), t3 result: <type ' Datetime.timedelta ' > Days, 12:00:00 5, Datetime.timedelta solve some time difference problem class Datetime.timedelta ([Days,[,hours[,minutes[,seconds[,microseconds]]]) dt= Datetime.timedelta (days=100) Newtime=t1+dt date after 100 days print newtime result: 2017-09-05 11:54:52.479000dt1= Datetime.timedelta (days=-100) newtime=T1+DT1 100 days ago Date 6, Total_seconds () converts the time to seconds Seconds=datetime.timedelta (days=3). Total_seconds () Convert three days to seconds print Seconds results: 259200.0
Python time: DateTime module