@nalan90
2017-07-31T03:52:42.000000Z
字数 3477
阅读 775
Python高效编程技巧实战
将字符串的时间转换为时间戳
In [1]: d = '2017-07-31 11:00:34'In [2]: import timeIn [7]: time.strptime?Docstring:strptime(string, format) -> struct_timeParse a string to a time tuple according to a format specification.See the library reference manual for formatting codes (same as strftime()).Type: builtin_function_or_methodIn [3]: timeArray = time.strptime(d,'%Y-%m-%d %H:%M:%S')In [4]: print timeArraytime.struct_time(tm_year=2017, tm_mon=7, tm_mday=31, tm_hour=11, tm_min=0, tm_sec=34, tm_wday=0, tm_yday=212, tm_isdst=-1)In [44]: time.mktime?Docstring:mktime(tuple) -> floating point numberConvert a time tuple in local time to seconds since the Epoch.Type: builtin_function_or_methodIn [5]: timestamp = int(time.mktime(timeArray))In [6]: print timestamp1501470034
字符串格式更改
## 如d = "2013-10-10 23:40:00",想改为 d = "2013/10/10 23:40:00"In [8]: dOut[8]: '2017-07-31 11:00:34'In [9]: timeArray = time.strptime(d,'%Y-%m-%d %H:%M:%S')In [10]: print timeArraytime.struct_time(tm_year=2017, tm_mon=7, tm_mday=31, tm_hour=11, tm_min=0, tm_sec=34, tm_wday=0, tm_yday=212, tm_isdst=-1)In [11]: print time.strftime('%Y/%m/%d %H:%M:%S',timeArray)2017/07/31 11:00:34In [12]: time.strftime?Docstring:strftime(format[, tuple]) -> stringConvert a time tuple to a string according to a format specification.See the library reference manual for formatting codes. When the time tupleis not present, current time as returned by localtime() is used.Type: builtin_function_or_method
时间戳转换为指定格式日期
## 方案一In [13]: time.time()Out[13]: 1501470399.986831In [14]: timestamp = int(time.time())In [15]: time.time?Docstring:time() -> floating point numberReturn the current time in seconds since the Epoch.Fractions of a second may be present if the system clock provides them.Type: builtin_function_or_methodIn [16]: print timestamp1501470419In [17]: time.localtime?Docstring:localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)Convert seconds since the Epoch to a time tuple expressing local time.When 'seconds' is not passed in, convert the current time instead.Type: builtin_function_or_methodIn [18]: timeArray = time.localtime(timestamp)In [19]: print timeArraytime.struct_time(tm_year=2017, tm_mon=7, tm_mday=31, tm_hour=11, tm_min=6, tm_sec=59, tm_wday=0, tm_yday=212, tm_isdst=0)In [20]: print time.strftime('%Y-%m-%d %H:%M:%S',timeArray)2017-07-31 11:06:59## 方案二In [23]: print timestamp1501470419In [24]: import datetimeIn [25]: dateArray = datetime.datetime.utcfromtimestamp(timestamp)In [26]: print dateArray2017-07-31 03:06:59In [27]: print type(dateArray)<type 'datetime.datetime'>In [28]: datetime.datetime.strftime?Docstring: format -> strftime() style string.Type: method_descriptorIn [30]: print dateArray.strftime('%Y-%m-%d %H:%M:%S')2017-07-31 03:06:59
获取当前时间并转换为指定日期格式
## 方案一In [61]: import timeIn [62]: now = int(time.time())In [63]: timeArray = time.localtime(now)In [64]: print time.strftime('%Y-%m-%d %H:%M:%S',timeArray)2017-07-31 11:28:14## 方案二In [65]: import datetimeIn [66]: now = datetime.datetime.now()In [67]: print now.strftime('%Y-%m-%d %H:%M:%S')2017-07-31 11:29:11
获得三天前的时间
In [38]: import time,datetimeIn [39]: threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3))In [46]: datetime.datetime.timetuple?Docstring: Return time tuple, compatible with time.localtime().Type: method_descriptorIn [40]: timestamp = int(time.mktime(threeDayAgo.timetuple()))## 生成时间戳In [41]: print timestamp1501211728## 生成字符串的日期In [43]: print threeDayAgo.strftime('%Y-%m-%d %H:%M:%S')2017-07-28 11:15:28
给定时间戳,计算该时间的几天前时间:
In [53]: timestamp = 1381419600In [54]: import time,datetimeIn [55]: dateArray = datetime.datetime.utcfromtimestamp(timestamp)In [56]: threeDayAgo = dateArray - datetime.timedelta(days = 3)In [57]: print threeDayAgo.strftime('%Y-%m-%d %H:%M:%S')2013-10-07 15:40:00