First look at how to convert a datetime type to the GMT time format string used for HTTP headers (such as ' Thu, Feb 2009 16:00:07 GMT '):
| The code is as follows |
Copy Code |
Import datetime Gmt_format = '%a,%d%b%Y%h:%m:%s GMT ' # The process of generating a DateTime object may be different from mine, and here is the current time to generate Datetime.datetime.utcnow (). Strftime (Gmt_format) |
Then see how to convert a string of GMT time format to a datetime type:
| The code is as follows |
Copy Code |
Time = ' Thu, Feb 2009 16:00:07 GMT ' Datetime.datetime.strptime (Time, Gmt_format) |
Finally, if you want to increase the output for a period of time, you can write it this way (for an additional 10 days):
| The code is as follows |
Copy Code |
(Datetime.datetime.strptime (time, Gmt_format) + Datetime.timedelta). Strftime (Gmt_format) |
In fact, the use of strftime and strptime of these 2 ways to transform each other.
| The code is as follows |
Copy Code |
Import time Print Time.time () #得到1395287736.81 Print Time.localtime (Time.time ()) #得到 time.struct_time (tm_year=2014, tm_mon=3, tm_mday=20, tm_hour=11, tm_min=55, tm_sec=36, tm_wday=3, tm_yday=79, tm_ isdst=0) Print Time.strftime ('%y-%m-%d%h:%i:%s ', Time.localtime (Time.time ()) #得到 2014-03-20 12:12:46 |
Ython3 code Example
1) Gets the string of the system's current time
| The code is as follows |
Copy Code |
Import datetime def get_formated_time (pstr= '%y-%m-%d%h:%m:%s '): Return Datetime.datetime.now (). Strftime (PSTR) If __name__== ' __main__ ': Print (Get_formated_time ()) |
2) Time Subtraction
| The code is as follows |
Copy Code |
From datetime import datetime A = ' 12:13:50 ' b = ' 12:28:21 ' Time_a = Datetime.strptime (A, '%h:%m:%s ') Time_b = Datetime.strptime (b, '%h:%m:%s ') Print (time_b-time_a). seconds |
1.time.time () Get 1970 to present time, unit seconds, have decimal point and this is Greenwich Mean time, all and China has 8 hours of jet lag
2. Use Time.localtime to get local time
3. If you want to obtain Time.time () China region time seconds can time.time () + 8 * 3600
4. Calculate the time difference, you can simply turn all the types of times into time.time () seconds, and then subtract.
Powerful time.strftime.
The following are the parameters of the Time.strftime:
Strftime (format[, tuple])-> string
Converts the specified struct_time (the default current time) to the specified formatted string output
Time date format symbol in Python:
%y Two-digit year representation (00-99)
%Y Four-digit year representation (000-9999)
%m Month (01-12)
Day of%d months (0-31)
%H 24-hour hours (0-23)
%I 12 Hours of hours (01-12)
%m minutes (00=59)
%s seconds (00-59)
%a Local Simplified Week name
%A Local Full week name
%b a locally simplified month name
%B Local Full month name
%c Local corresponding date representation and time representation
%j Day of the Year (001-366)
%p the equivalent of a local a.m. or p.m.
%u number of weeks in a year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday for the beginning of the week
%w number of weeks in a year (00-53) Monday is the beginning of the week
%x Local corresponding date representation
%x Local corresponding time representation
%Z the name of the current time zone
%%% per se