Python Time processing datetime

Source: Internet
Author: User
Tags instance method local time
operating Environment: Python3.4

 #-*-Coding:utf-8-*-#datetime类 #datetime是date与time的结合体, including all information about date and time. #它的构造函数如下: #datetime. DateTime (year, month, day[, hour[, minute[, second[, microsecond[, Tzinfo]]] #各参数的含义与date, as in the constructor of time, be aware




The range of parameter values. # 1. 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.max: ' +str (Datetime.max)) print (' Datetime.min: ' +str (datetime.min)) #datetime. Resolution:datetime minimum Unit; #print: DATETIME.R esolution:0:00:00.000001 print (' datetime.resolution: ' + str (datetime.resolution)) #datetime. Today () : Returns a DateTime object that represents the current local time; #print: Today (): 2012-09-12 19:37:50.721000 print (' Today (): ' +str (Datetime.today ()) #datet Ime.now ([TZ]): Returns a DateTime object that represents the current local time, and if a parameter tz is supplied, 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 date of the current UTC timeTime object; #print: 2012-09-12 11:37:50.739000 print (' UtcNow (): ' +str (Datetime.utcnow ()) #datetime. Fromtimestamp (timest amp[, TZ]: Creates a DateTime object based on the timestamp, parameter TZ Specifies the 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 the 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))) #datetime. 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 instance method provided by the DateTime class and the attribute 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 (d T.second) print (dt.microsecond) print (dt.tzinfo) print (Dt.date ()) print (Dt.time ()) Print (Dt.replace (year = 2013)) Prin T (dt.timetuple ()) print (Dt.utctimetuple ()) print (Dt.toordinal ()) print (Dt.weekday ()) print (Dt.isocalendar ()) #print Dt.isoformat ([Sep]) #datetime. CTime (): Returns a date-time C format string that is equivalent to Time.ctime (Time.mktime (Dt.timetuple ()); #3. Format string # DateTime. Strftime (format) #%a week's abbreviation. As Wednesday for the Web #%A week full write. For example, Wednesday is the shorthand for Wednesday #%b month. such as April for the APR #%b month Full write. such as the string representation of the April #%c: DateTime in April. (For example: 04/07/10 10:43:39) #%d: Days in this month (is the day of the months) #%f: microseconds (range [0,999999]) #%H: Hours (24 hours, [0,]) #%I: Hours (12 hours, [0, 11] ) #%j: Days in the year [001,366] (is day of the year) #%m: Month ([01,12]) #%m: minutes ([00,59]) #%p:am or PM #%s: seconds (range is [00,61], why not [00, 59],  Reference to the Python manual ~_~) #%u: Weeks in the year of the Week of the year of the first few weeks, Sunday as the first day of the week #%w: Today in this week's number of days, the range is [0, 6],6 represents Sunday #%w: Weeks in the current year (is the week of the year), Monday as the first day of the Week #%x: Date string (such as: 04/07/10) # %x: Time string (such as: 10:43:39) #%y:2 number of years #%y:4 number of years #%z: Interval from UTC (If local time, return an empty string) #%z: Time zone name (if local time, return an empty string) # 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.strftime ('%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 ')) #pri NT:%a:wednesday print ('%%a:%s '% dt.strftime ('%A ') #print:%b:sep print ('%%b:%s '% dt.strftime ('%b ')) #print:% B:september 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 ('%%x:%s '% dt.strftime ('%x ')) #print: Time%x:23:04:27 print (' Time%%x:%s '% Dt.strftim E ('%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 the first%s day '% of this year Dt.strfti Me ('%j ') #print: This week is this year'sWeek 37th print (' This is this week's%s week '% dt.strftime ('%u ')]
 


Run Results

atetime.max:9999-12-31 23:59:59.999999
datetime.min:0001-01-01 00:00:00
datetime.resolution:0:0 0:00.000001 Today
(): 2014-05-04 15:58:18.141186 now
(): 2014-05-04 15:58:18.193146 utcnow
(): 2014-05-04 07:58:18.243958
Fromtimestamp (tmstmp): 2014-05-04 15:58:18.291558 utcfromtimestamp
(tmstmp): 2014-05-04 07:58:18.342550
Datetime.combine (date,time): 2012-09-12 19:46:05 2007-03-04 21:08:12
9
8
0
None
2012-09-12
21:08:12
2013-09-12 21:08:12
Time.struct_time (tm_year=2012, tm_mon=9, tm_mday=12, tm_hour=21, Tm_min=8, tm_sec=12, tm_wday=2, tm_yday=256, TM_ISDST =-1)
time.struct_time (tm_year=2012, tm_mon=9, tm_mday=12, tm_hour=21, Tm_min=8, tm_sec=12, tm_wday=2, tm_yday= 256, tm_isdst=0)
734758
2 (
3)
(%y-%m-%d%h:%m:%s%f): 2014-05-04 15:58:19
(% y-%m-%d%h:%m:%s%p): 14-05-04 03:58:19 PM
%a:sun 
%a:sunday%b:may%b:may 
Date Time%c:sun may
  4 15:58:19 2014 
date%x:05/04/14 
time%x:15:58:19 
today is the No. 0 day of the week 
today is the 124th day of this year 




operating Environment: python2.x


 #-*-Coding:utf-8-*-#datetime类 #datetime是date与time的结合体, including all information about date and time. #它的构造函数如下: #datetime. DateTime (year, month, day[, hour[, minute[, second[, microsecond[, Tzinfo]]] #各参数的含义与date, as in the constructor of time, be aware




The range of parameter values. # 1. 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.max: ', Datetime.max print ' datetime.min: ', datetime.min #datetime. resolution:datetime smallest unit; #print: datetime.resolution:0 : 00:00.000001 print ' datetime.resolution: ', Datetime.resolution #datetime. Today (): Returns a DateTime object that represents the current local time; #print: Today (): 2012-09-12 19:37:50.721000 print ' Today (): ', Datetime.today () #datetime. Now ([TZ]): Returns a DateTime object that represents the current local time. Gets the local time of the time zone that the TZ parameter refers to, if the parameter tz is provided; #print: Now (): 2012-09-12 19:37:50.738000 print ' Now (): ', DateTime.Now () #datetime. UtcNow ( ): Returns a DateTime object for the current UTC time; #print: 2012-09-12 11:37:50.739000 print ' UtcNow (): ', Datetime.utcnow () #datetime. Fromtimestamp (timestamp[, TZ]): Creates a DateTime object based on the timestamp, Parameter TZ Specify the time zone information; #print: Fromtimestamp (tmstmp): 2012-09-12 19:37:50.741000 print ' Fromtimestamp (tmstmp): ', Datetime.fromtimestamp (Time.time ()) #datetime. Utcfromtimestamp (timestamp): Creates a DateTime object based on the timestamp; #print: Utcfromtimestamp (tmstmp): 2012-09-12 11:37:50.742000 print ' Utcfromtimestamp (tmstmp): ', 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): '
, Datetime.combine (d,t) #datetime. 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. Instance methods and properties provided by the DateTime class dt = Datetime.strptime ("2012-09-12 21:08:12", "%y-%m-%d%h:%m:%s") #print: 9 8 0 None PR int Dt.year,dt.montH,dt.day,dt.hour,dt.minute,dt.second,dt.microsecond,dt.tzinfo print dt.date () print dt.time () print dt.replace (year = 2013) Print dt.timetuple () print dt.utctimetuple () print dt.toordinal () print dt.weekday () #print D T.isoformat ([Sep]) #datetime. CTime (): Returns a date-time C format string that is equivalent to Time.ctime (Time.mktime (Dt.timetuple ()); #3. Format string # DateTime. Strftime (format) #%a week's abbreviation. As Wednesday for the Web #%A week full write. For example, Wednesday is the shorthand for Wednesday #%b month. such as April for the APR #%b month Full write. such as the string representation of the April #%c: DateTime in April. (For example: 04/07/10 10:43:39) #%d: Days in this month (is the day of the months) #%f: microseconds (range [0,999999]) #%H: Hours (24 hours, [0,]) #%I: Hours (12 hours, [0, 11] ) #%j: Days in the year [001,366] (is day of the year) #%m: Month ([01,12]) #%m: minutes ([00,59]) #%p:am or PM #%s: seconds (range is [00,61], why not [00, 59],  Reference to the Python manual ~_~) #%u: Weeks in the year of the Week of the year of the first few weeks, Sunday as the first day of the week #%w: Today in this week's number of days, the range is [0, 6],6 represents Sunday #%w: Weeks in the current year (is the week of the year), Monday as the first day of the Week #%x: Date strings (such as: 04/07/10) #%x: Time strings (such as: 10:43:39) #%y:2 number of years #%y:4 number of years #%z: Interval from UTC (If local time, return an empty string) #%z: Time zone name Call (if local time, return an empty string) #%%:  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): ', Dt.strftime ('%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): ', Dt.strftime ('%y-%m-%d%i:%m:%s%p ') #print:%a:wed print '%%a:%s '% dt.strftime ('%a ') #print:%a:wednesday p Rint '%%a:%s '% dt.strftime ('%A ') #print:%b:sep print '%%b:%s '% dt.strftime ('%b ') #print:%b:september print '%%b :%s '% dt.strftime ('%B ') #print: Date time%C:09/12/12 23:04:27 print ' Date time%%c:%s '% dt.strftime ('%c ') #print: Date%X:09/12/1 2 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 Prin T ' today is the first%s day of the week '% 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 the 37th week of this year print

 ' This week is the first%s week of this year '% dt.strftime ('%u ')


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.