#-*-Coding:utf-8-*-#datetime类 #datetime是date与time的结合体, including all information on date and time. #它的构造函数如下: #datetime. DateTime (year, month, day[, hour[, minute[, second[, microsecond[, Tzinfo]]]]) #各参数的含义与date, as in the constructor of time, note The range of the value of the parameter. # 1. The class properties and methods defined by the DateTime class: #datetime. Min, datetime.max:datetime can represent the minimum and maximum value; #print: datetime.max:9999-12-31 23:59:59.999999 #print: datetime.min:0001-01-01 00:00:00 from datetime import * Import time print (' Datetime.ma x: ' +str (Datetime.max)) print (' Datetime.min: ' +str (datetime.min)) #datetime. Resolution:datetime minimum Unit; #print: Dat etime.resolution:0:00:00.000001 print (' datetime.resolution: ' + str (datetime.resolution)) #datetime. Today (): Returns a representation of when DateTime object of the former local time; #print: Today (): 2012-09-12 19:37:50.721000 print (' Today (): ' +str (Datetime.today ())) #datetime. No W ([TZ]): Returns a DateTime object that represents the current local time, and if parameter TZ is provided, gets the local time of the time zone referred to by the TZ parameter; #print: Now (): 2012-09-12 19:37:50.738000 print (' Now (): ' +str (DateTime.Now ())) #datetiMe.utcnow (): Returns a DateTime object for the current UTC time; #print: 2012-09-12 11:37:50.739000 print (' UtcNow (): ' +str (Datetime.utcnow ())) #datetime. Fromtimestamp (timestamp[, TZ]): Creates a DateTime object based on time timestamp, parameter TZ specifies time zone information, #print: Fromtimestamp (tmstmp): 2012-09-12 19:37:50.741000 print (' Fromtimestamp (tmstmp): ' +str (Datetime.fromtimestamp (Time.time ())) # Datetime.utcfromtimestamp (timestamp): Creates a DateTime object based on time timestamp; #print: Utcfromtimestamp (tmstmp): 2012-09-12 11:37:50.742000 print (' Utcfromtimestamp (tmstmp): ' +str (Datetime.utcfromtimestamp (Time.time ()))) #datetime. Combine (date, time): Creates a DateTime object based on date and time; #print: Datetime.combine (date,time): 2012-09-12 19:46:05 d = Date (2012,9,12) from datetime import * t = time (19,46,5) print (' Datetime.combine (date,time): ' +str (Datetime.combine (d,t))) #dateti Me.strptime (date_string, format): Converts a format string to a DateTime object; #print: 2007-03-04 21:08:12 print (Datetime.strptime (" 2007-03-04 21:08:12 ","%y-%m-%d%h:%m:%s ")) #2. The DateTime class provides an instance method with a property of dt = DateTime.Strptime ("2012-09-12 21:08:12", "%y-%m-%d%h:%m:%s") #print: 9 8 0 None print (dt.year) print (dt.month) Print (dt.day) print (dt.hour) print (dt.minute) print (dt.second) print (dt.microsecond) print (dt.tzinfo) print (dt.date ()) Print (Dt.time ()) Print (dt.replace (year =)) print (Dt.timetuple ()) print (Dt.utctimetuple ()) Print (Dt.toor Dinal ()) print (Dt.weekday ()) print (Dt.isocalendar ()) #print dt.isoformat ([Sep]) #datetime. CTime (): Returns a C-format string of datetime equivalent to Time.ctime (Time.mktime (Dt.timetuple ())); #3. The format string # DateTime. Strftime (format) # Shorthand for%a week. Like Wednesday for the full write of Web #%A week. such as Wednesday for the Wednesday #%b month abbreviation. Like April for Apr #%b Month Full write. As in April, the string representation of April #%c: DateTime. (Example: 04/07/10 10:43:39) #%d: Days in this month (the day of the Week) #%f: microseconds (range [0,999999]) #%H: Hours (24-hour, [0]) #%I: Hours (12-hour, [0, ] #%j: Number of days in the year [001,366] (the day of the current year) #%m: Month ([01,12]) #%m: minutes ([00,59]) #%p:am or PM #%s: seconds (range = [00,61], why not [00, 59], refer to Python manual ~_~) #%u: Week in the Year of the Week of the year, Sunday as the first day of the Week #%w: Today, the number of days in this week, the range is [0, 6],6 = Sunday #%W: Week in the year (is the week of the year), Monday as the first day of the week #%x: date string (e.g.: 04/07/10) #%x: time string (e.g. 10:43:39) #%y: 2 numbers represents the year #%y:4 number represents the year #%z: Interval from UTC (If local time, returns an empty string) #%z: Time zone name (returns an empty string if local time) # percent:%% =% DT = DateTime.Now () #print: (%y-%m-%d%h:%m:%s%f): 2012-09-12 23:04:27 145000 print (' (%y-%m-%d%h:%m:%s%f): ' + str (dt.str Ftime ('%y-%m-%d%h:%m:%s%f ')) #print: (%y-%m-%d%h:%m:%s%p): 12-09-12 11:04:27 PM print (' (%y-%m-%d%h:%m:%s%p): ' + STR (dt.strftime ('%y-%m-%d%i:%m:%s%p ')) #print:%a:wed print ('%%a:%s '% dt.strftime ('%a ')) #print:%a:wednesd ay print ('%%a:%s '% dt.strftime ('%A ')) #print:%b:sep print ('%%b:%s '% dt.strftime ('%b ')) #print:%b:septembe R print ('%%b:%s '% dt.strftime ('%B ')) #print: DateTime%c:09/12/12 23:04:27 print (' DateTime%%c:%s '% dt.strftime ('%c ')) #print: Date%X:09/12/12 print (' date%%x:%s '% dt.strftime ('%x ') #print: Time%x:23:04:27 print (' Time%%x:%s '% dt.strftime ('% X ') #print: Today is the 3rd day of the Week Print (' Today is this week's%s day '% dt.strftime ('%w ')) #print: Today is the NO. 256 day of this year print (' Today is this year's%s day '% dt.strftime ('%j ')) #print: This week is this year The 37th Week of print (' This week is this year's%s week '% dt.strftime ('%u '))
DateTime of Python time processing