There are a few doubts about datetime and time.
1, Datetime.datetime.now ()--Why two DateTime is required to return the current time, only need to Time.localtime ()
And then I got it. Datetime.datetime.now ()--The first datetime is the name of the Py file, and the middle datetime is the class name, now is the method
2, formatted output "%h%m%s", is also formatted output, why one is Datetime.datetime.strftime ("%h%m%s"), the other is Time.strftime ("%h%m%s", Time.localtime ( ))
Note that Datetime.datetime.strftime is a method of the class, Note that Datetime.datetime.now () returns a DateTime instantiation object. So you can use the Datetime.datetime.strftime method directly
and Time.strftime () is the method of the time module, note thatTime.localtime () returns the Time.struct_time object, this object is not strftime method of natural error, Usage time.strftime (format, time)
--------------------------------------I'm a split line--------------------------------------
Let's go on to talk about some of the ways to find the last year's number of years.
Use the Add and subtract method to Datetime.timedelta date, and Calendar.monthrange () method to get the number of days of the month
1, first of all, respectively, construction
this month, number 1th datetime--date_now = Datetime.datetime (Year=year, Month=month, Day=1) # constructs datetime 1th this month
DateTime for the last day of the month
2, because the Timedelta maximum support only to the days parameter, this month 1th minus 1 is the last day of last month, you can get a definite last month value; the last day of the month +1 is the first day of next month .
3, repeated calls, return the corresponding month can be
4, does not add the day's parameters, mainly the uncertainty of the day did not want to understand how to do better, such as 20160229 of the previous year of the number should be how to write, if there are ideas of the partner may wish to enlighten
#!/usr/bin/env python#-*-coding:utf-8-*-# datetime:2018/7/13 0:54# author:xzs "" "Function: incoming date similar to" 201807 "format, year and month parameters, such as Date _before ("201807", Year=1, month=7)-Return to the date of the previous year July, get "201612" Date_after ("201807", Year=1, Month=6)--return to the date of June after the same year, get " 202001 "Date_before (" 201807 ", Year=1, month=0)--year-earlier" "" Import datetimefrom datetime import Timedeltaimport calendarimp ORT sysreload (SYS) sys.setdefaultencoding ("Utf-8") # returns the last month of the incoming date def last_one_month (date): year = Int (Date[:4]) month = i NT (date[4:]) Date_now = Datetime.datetime (Year=year, Month=month, Day=1) # Constructed this month 1th datetime date_last_month = date_no W-timedelta (Days=1) # last month datetime return Date_last_month.strftime ("%y%m") # returns the next month of incoming date def next_one_month (date): Ye AR = Int (date[:4]) month = Int (date[4:]) A, B = calendar.monthrange (year, month) # A,b--weekday the first day of the week (0-6 corresponds Monday to week) Days) and all days of the month Date_now = Datetime.datetime (Year=year, Month=month, day=b) # Constructed this month 1th datetime date_next_month = Date_now + Timedelta (Days=1) # last month dateTime return Date_next_month.strftime ("%y%m") def date_before (date, Year=none, month=none): Print U "%s year%s month ago date is:"% (Y Ear if year else "-", month if month Else '-"), if year >= 1:month = * year + month if month > 1: For M in range (1, month + 1): New_date = Last_one_month (date) # returns last month, and then the last month, based on the loop calculation to get the final monthly date = New_date elif Month = = 1:new_date = Last_one_month (date) elif month = = 0:new_date = Date # if not The input parameter, which returns this date by default if year was None and month is none:new_date = date print new_date return new_datedef Date_aft ER (date, year=none, month=none): Print U "%s" date after%s months is: "% (year if of the Else"-", month if month else"-"), if annual & gt;= 1:month = * year + month if month > 1:for m in range (1, month + 1): New_date = NE Xt_one_month (date) # returns to the next month, based on the next month, the Loop calculates the final month date = new_date elif month = = 1:new_date = Next_one_m Onth (date) elif month = = 0:new_date = Date # If you do not enter a parameter, the default is to return this date if year was None and month is none:new_date = date Print n Ew_date return New_dateif __name__ = = ' __main__ ': # next_day ("20180501", day=5) # last_day ("20160301", day=1,year=5 ) Date_before ("201801") date_after ("201807")
Python datetime and time some doubts and get the same period last year, month, etc date