[关闭]
@nalan90 2017-07-31T11:52:42.000000Z 字数 3477 阅读 626

专题八 时间处理常用操作

Python高效编程技巧实战


将字符串的时间转换为时间戳
  1. In [1]: d = '2017-07-31 11:00:34'
  2. In [2]: import time
  3. In [7]: time.strptime?
  4. Docstring:
  5. strptime(string, format) -> struct_time
  6. Parse a string to a time tuple according to a format specification.
  7. See the library reference manual for formatting codes (same as strftime()).
  8. Type: builtin_function_or_method
  9. In [3]: timeArray = time.strptime(d,'%Y-%m-%d %H:%M:%S')
  10. In [4]: print timeArray
  11. time.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)
  12. In [44]: time.mktime?
  13. Docstring:
  14. mktime(tuple) -> floating point number
  15. Convert a time tuple in local time to seconds since the Epoch.
  16. Type: builtin_function_or_method
  17. In [5]: timestamp = int(time.mktime(timeArray))
  18. In [6]: print timestamp
  19. 1501470034

字符串格式更改
  1. ## 如d = "2013-10-10 23:40:00",想改为 d = "2013/10/10 23:40:00"
  2. In [8]: d
  3. Out[8]: '2017-07-31 11:00:34'
  4. In [9]: timeArray = time.strptime(d,'%Y-%m-%d %H:%M:%S')
  5. In [10]: print timeArray
  6. time.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)
  7. In [11]: print time.strftime('%Y/%m/%d %H:%M:%S',timeArray)
  8. 2017/07/31 11:00:34
  9. In [12]: time.strftime?
  10. Docstring:
  11. strftime(format[, tuple]) -> string
  12. Convert a time tuple to a string according to a format specification.
  13. See the library reference manual for formatting codes. When the time tuple
  14. is not present, current time as returned by localtime() is used.
  15. Type: builtin_function_or_method

时间戳转换为指定格式日期
  1. ## 方案一
  2. In [13]: time.time()
  3. Out[13]: 1501470399.986831
  4. In [14]: timestamp = int(time.time())
  5. In [15]: time.time?
  6. Docstring:
  7. time() -> floating point number
  8. Return the current time in seconds since the Epoch.
  9. Fractions of a second may be present if the system clock provides them.
  10. Type: builtin_function_or_method
  11. In [16]: print timestamp
  12. 1501470419
  13. In [17]: time.localtime?
  14. Docstring:
  15. localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
  16. tm_sec,tm_wday,tm_yday,tm_isdst)
  17. Convert seconds since the Epoch to a time tuple expressing local time.
  18. When 'seconds' is not passed in, convert the current time instead.
  19. Type: builtin_function_or_method
  20. In [18]: timeArray = time.localtime(timestamp)
  21. In [19]: print timeArray
  22. time.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)
  23. In [20]: print time.strftime('%Y-%m-%d %H:%M:%S',timeArray)
  24. 2017-07-31 11:06:59
  25. ## 方案二
  26. In [23]: print timestamp
  27. 1501470419
  28. In [24]: import datetime
  29. In [25]: dateArray = datetime.datetime.utcfromtimestamp(timestamp)
  30. In [26]: print dateArray
  31. 2017-07-31 03:06:59
  32. In [27]: print type(dateArray)
  33. <type 'datetime.datetime'>
  34. In [28]: datetime.datetime.strftime?
  35. Docstring: format -> strftime() style string.
  36. Type: method_descriptor
  37. In [30]: print dateArray.strftime('%Y-%m-%d %H:%M:%S')
  38. 2017-07-31 03:06:59

获取当前时间并转换为指定日期格式
  1. ## 方案一
  2. In [61]: import time
  3. In [62]: now = int(time.time())
  4. In [63]: timeArray = time.localtime(now)
  5. In [64]: print time.strftime('%Y-%m-%d %H:%M:%S',timeArray)
  6. 2017-07-31 11:28:14
  7. ## 方案二
  8. In [65]: import datetime
  9. In [66]: now = datetime.datetime.now()
  10. In [67]: print now.strftime('%Y-%m-%d %H:%M:%S')
  11. 2017-07-31 11:29:11

获得三天前的时间
  1. In [38]: import time,datetime
  2. In [39]: threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3))
  3. In [46]: datetime.datetime.timetuple?
  4. Docstring: Return time tuple, compatible with time.localtime().
  5. Type: method_descriptor
  6. In [40]: timestamp = int(time.mktime(threeDayAgo.timetuple()))
  7. ## 生成时间戳
  8. In [41]: print timestamp
  9. 1501211728
  10. ## 生成字符串的日期
  11. In [43]: print threeDayAgo.strftime('%Y-%m-%d %H:%M:%S')
  12. 2017-07-28 11:15:28

给定时间戳,计算该时间的几天前时间:
  1. In [53]: timestamp = 1381419600
  2. In [54]: import time,datetime
  3. In [55]: dateArray = datetime.datetime.utcfromtimestamp(timestamp)
  4. In [56]: threeDayAgo = dateArray - datetime.timedelta(days = 3)
  5. In [57]: print threeDayAgo.strftime('%Y-%m-%d %H:%M:%S')
  6. 2013-10-07 15:40:00
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注