Datetime in Python Time Processing

Source: Internet
Author: User
#-*-Coding: UTF-8-*-# datetime class # datetime is a combination of date and time, including all information about date and time. # Its constructor is as follows: # datetime. datetime (year, month, day [, hour [, minute [, second [, microsecond [, tzinfo]) # The meaning of each parameter is the same as that in the date and time constructor. Pay attention to the parameter value range. #1. the class attributes and methods defined by the datetime class: # datetime. min, datetime. MAX: minimum and maximum values that can be expressed by datetime; # print: datetime. MAX: 9999-12-31 23:59:59. 999999 # print: datetime. min: 0001-01-01 00: 00: 00 from datetime import * import timeprint 'datetime. MAX: ', datetime. max print 'datetime. min: ', datetime. min # datetime. resolution: Minimum datetime unit; # print: datetime. resolution: 0:00:00. 000001 print 'datetime. resolution: ', datetime. resolution # dat Etime. today (): returns a datetime object indicating the current local time; # print: Today (): 19:37:50. 721000 print 'today (): ', datetime. today () # datetime. now ([TZ]): returns a datetime object that represents the current local time. If the TZ parameter is provided, obtain the local time in the time zone specified by the TZ parameter; # print: Now (): 19:37:50. 738000 print 'now (): ', datetime. now () # datetime. utcnow (): returns a datetime object of the current UTC time; # print: 11:37:50. 739000 print 'utcnow (): ', datetime. utcnow () # datetime. fromtimestamp (T Imestamp [, TZ]): Creates a datetime object based on the time period. The parameter TZ specifies the time zone information; # print: fromtimestamp (tmstmp): 19:37:50. 741000 print 'fromtimestamp (tmstmp): ', datetime. fromtimestamp (time. time () # datetime. utcfromtimestamp (timestamp): Create a datetime object based on the time sequence; # print: utcfromtimestamp (tmstmp): 11:37:50. 742000 print 'utcfromtimestamp (tmstmp): ', datetime. utcfromtimestamp (time. time () # datetime. combine (date, time): According to DA Te and time, create a datetime object; # print: datetime. combine (date, time): 2012-09-12 19: 46: 05d = Date (, 12) from datetime import * t = time (, 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. the instance method and attribute dt = datetime provided by the datetime class. strptime ("20 12-09-12 21:08:12 "," % Y-% m-% d % H: % m: % s ") # print: 2012 9 12 21 8 12 0 noneprint DT. year, DT. month, DT. day, DT. hour, DT. minute, DT. second, DT. microsecond, DT. tzinfoprint DT. date () print DT. time () print DT. replace (year = 2013) print DT. timetuple () print DT. utctimetuple () print DT. toordinal () print DT. weekday () print DT. isocalendar () # print DT. isoformat ([Sep]) # datetime. ctime (): returns a C-format string of date and time, which is equivalent to time. ctime (time. MKT IME (Dt. timetuple (); #3. Format String # datetime. strftime (Format) # % A abbreviation of the week. For example, Wednesday is the full write of Web # %. For example, Wednesday # % B is short for Wednesday. For example, if February is Apr # % B. For example, February April is a string representation of L # % C: Date and time. (For example, 04/07/10 10:43:39) # % d: the number of days in the month (the day of the month) # % F: microsecond (in the range of []) # % H: hour (in 24-hour format, [0, 23]) # % I: hour (in 12-hour format, [0, 11]) # % J: days in the year [001,366] (the day of the year) # % m: Month ([]) # % m: minute ([]) # % P: AM or PM # % s: Second (range: [], why not [00, 59], refer to the python manual ~ _~) # % U: The Week in the current year's week number in the current year), Sunday as the first day of the week # % W: the number of days of today in this week, the range is [0, 6], 6 indicates Sunday # % W: the number of weeks in the current year (the week of the current year), and Monday as the first day of the week # % x: Date string (for example, 04/07/10) # % x: Time string (for example, 10: 43: 39) # % Y: year represented by 2 digits # % Y: year represented by 4 digits # % Z: the interval between the time and UTC (if the local time is used, an empty string is returned) # % Z: Time Zone name (if the local time is used, an empty string is returned) # %: % => % dt = datetime. now () # print: (% Y-% m-% d % H: % m: % S % F ): 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 pmprint '(% Y-% m-% d % H: % m: % S % P ):', DT. strftime ('% Y-% m-% d % I: % m: % S % p') # print: % A: Wed print' %: % s' % DT. strftime ('% A') # print: % A: wednesdayprint' % A: % s' % DT. strftime ('% A') # print: % B: SEP print' % B: % s' % DT. strftime ('% B') # print: % B: septemberprint '% B: % s' % DT. strftime ('% B') # print: Date and Time % C: 09/12/12 23: 04: 27 print 'date and time % 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 the % s day of the week' % DT. strftime ('% W') # print: Today is the 256th day of this year. Print' today is the % s day of this year '% DT. strftime ('% J') # print: this week is the 37th week of this year print' this week is the % s week of this year '% DT. strftime ('% U') -- EOF -- Author: Once-ler | http://blog.csdn.net/wirelessqamailbox: wirelessqa.me@gmail.com Weibo: http://weibo.com/wirelessqa reprinted please indicate Source: Once-ler-http://blog.csdn.net/wirelessqa
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.