Start from a string
>>>time_str= ' 2008-08-08 08:08:08 '
1.1. Convert to struct_time form of time
>>struct = Ime.strptime (time_str, '%y-%m-%d%h:%m:%s ') time.struct_time (tm_year=2008, tm_mon=8, Tm_mday=8, Tm_hour=8, Tm_min=8, Tm_sec=8, tm_wday=4, tm_yday=221, Tm_isdst=-1)
1.2. If you want to get the corresponding timestamp (in seconds):
>>>sec=time.mktime (struct) >>> sec1218154088.0
1.3.struct_time The time of the form to return the starting string:
1.4. Timestamp (seconds) What happens when I return to the Struct_time form?
<pre name= "code" class= "Python" >>> time.gmtime (sec) time.struct_time (tm_year=2008, tm_mon=8, Tm_mday=8, Tm_hour=0, Tm_min=8, Tm_sec=8, tm_wday=4, tm_yday=221, tm_isdst=0)
1.5. Timestamp (number of seconds) to return to the string should know how to do it? Of course, there is a direct way, but the conversion back to the time format is not the same:
1.6. Want to get the current time:
Today:
>>> datetime.date.today () datetime.date (2015, 4, 3)
Right now:
>>> Datetime.datetime.now () datetime.datetime (2015, 4, 3, 15, 19, 47, 361000)
Time Stamp Now:
>>> Time.time ()
1428045689.396
Now the struct_time form time:
>>> Time.localtime ()
Time.struct_time (tm_year=2015, tm_mon=4, tm_mday=3, tm_hour=15, tm_min=21, tm_sec=52, tm_wday=4, tm_yday=93, tm_isdst= 0)
The current UTC date form:
>>> Time.ctime ()
' Fri Apr 03 15:23:45 2015 '
1.7)datetime.date/datetime/time to convert to struct_time ?
>>> Datetime.datetime.now (). Timetuple ()
Time.struct_time (tm_year=2015, tm_mon=4, tm_mday=3, tm_hour=15, tm_min=31, tm_sec=19, tm_wday=4, tm_yday=93, tm_isdst= -1)
So, with 1.2, is it easy to convert to seconds?
1.8 datetime.date/datetime 2010-01-01 00 : 00 : 00 " the form of a string?
Isn't it easy to combine 1.3 and 1.7 ?
1.9. How do strings convert to datetime.date/datetime/time ?
>>> datetime.datetime.strptime (' 2014-01-01 ', "%y-%m-%d") datetime.datetime (2014, 1, 1, 0, 0)
2.0. and then we're going to convert struct_time to datetime.date/datetime/time .
Under what circumstances you need to convert struct_time to datetime.date/datetime/time. See the 2.1 I understand.
2.1 Time Arithmetic--the addition and subtraction of time
How did the time of yesterday count?
>> Today=datetime.date.today ()
>>> Delta=datetime.timedelta (Days=1) >>> yesterday=today-delta >>> yesterday
What about tomorrow? After seven days (before)? What about a minute ago (), one second?
Look at this constructor:
Class Datetime.timedelta ([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, Weeks]] []]), can you help answer the above questions?
Note thatstruct_time and strings are not allowed to operate with Datetime.timedelta . So know how to convert from other forms into datetime.date/datetime/time. It's very useful.
2.2) time comparison:
This is only one sentence:datetime. ( Date/datetime/time.) and the Struct_time the time of the form can be compared. (cannot be compared to each other)
Super-Practical Python date processing notes