#! /Usr/bin/python # Coding = UTF-8 Import datetime """ Powerful functions of datetime Supported from 0001 to 9999 """ """ Current Time Returns a datetime type. The now method has a parameter tz to set the time zone type. If the effect is not the same as that of the today method """ Now = datetime. datetime. now () # UTC time Datetime. datetime. utcnow () Attrs = [ ("Year", "year"), ('month', "month'), (" day "," day "), ('hour", "hour "), ('minute ', "minute"), ('second', "second"), ('microsecond', "millisecond "),( 'Min', "min"), ('max ', "max "), ] For k, v in attrs: "Now. % s = % s # % s" % (k, getattr (now, k), v) """ Returns a time structure. """ Now. timetuple () """ Returns a date type. """ Now. date () """ Returns a time type. """ Now. time () """ The current day of the week. Monday is 0, and the week is 6 Note that the method is not an attribute. """ Now. weekday () """ The current day of the week. Monday is 1, week is 7 Note that the method is not an attribute. """ Now. isoweekday () """ Modify the current time. For example, change it to the 1st day of the current month. """ Now. replace (day = 1) Past = datetime. datetime (2010,11, 12,13, 14,15, 16) """ Comparison The returned value is of the timedelta type. """ Now-past """ Convert to string For detailed rules, see Time """ Strdatetime = now. strftime ("% Y-% m-% d % H: % M: % S ") """ String generation datetime object """ Datetime. datetime. strptime (strdatetime, "% Y-% m-% d % H: % M: % S ") |