[关闭]
@StrGlee 2016-10-19T22:15:02.000000Z 字数 3318 阅读 1017

python 时间函数

python

时间函数

1. datetime

  1. import datetime
  2. now = datetime.datetime.now()
  3. now
  4. datetime.datetime(2015, 1, 12, 23, 9, 12, 946118)
  5. type(now)
  6. <type 'datetime.datetime'>

2. timestamp

  1. import time
  2. time.time()
  3. 1421075455.568243

3. time tuple

  1. import time
  2. time.localtime()
  3. time.struct_time(tm_year=2015, tm_mon=1, tm_mday=12, tm_hour=23, tm_min=10, tm_sec=30, tm_wday=0, tm_yday=12, tm_isdst=0)

4. string

  1. import datetime
  2. datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  3. '2015-01-12 23:13:08'

5. date

  1. import datetime
  2. datetime.datetime.now().date()
  3. datetime.date(2015, 1, 12)

datetime基本操作

1. 获取当前datetime

  1. import datetime
  2. datetime.datetime.now()
  3. datetime.datetime(2015, 1, 12, 23, 26, 24, 475680)

2. 获取当天date

  1. datetime.date.today()
  2. datetime.date(2015, 1, 12)

3. 获取明天/前N天

明天

  1. datetime.date.today() + datetime.timedelta(days=1)
  2. datetime.date(2015, 1, 13)

三天前

  1. datetime.datetime.now()
  2. datetime.datetime(2015, 1, 12, 23, 38, 55, 492226)
  3. datetime.datetime.now() - datetime.timedelta(days=3)
  4. datetime.datetime(2015, 1, 9, 23, 38, 57, 59363)

4. 获取当天开始和结束时间(00:00:00 23:59:59)

  1. datetime.datetime.combine(datetime.date.today(), datetime.time.min)
  2. datetime.datetime(2015, 1, 12, 0, 0)
  3. datetime.datetime.combine(datetime.date.today(), datetime.time.max)
  4. datetime.datetime(2015, 1, 12, 23, 59, 59, 999999)

5. 获取两个datetime的时间差

  1. (datetime.datetime(2015,1,13,12,0,0) - datetime.datetime.now()).total_seconds()
  2. 44747.768075

6. 获取本周/本月/上月最后一天

本周

  1. today = datetime.date.today()
  2. today
  3. datetime.date(2015, 1, 12)
  4. sunday = today + datetime.timedelta(6 - today.weekday())
  5. sunday
  6. datetime.date(2015, 1, 18)

本月

  1. import calendar
  2. today = datetime.date.today()
  3. _, last_day_num = calendar.monthrange(today.year, today.month)
  4. last_day = datetime.date(today.year, today.month, last_day_num)
  5. last_day
  6. datetime.date(2015, 1, 31)

获取上个月的最后一天(可能跨年)

  1. import datetime
  2. today = datetime.date.today()
  3. first = datetime.date(day=1, month=today.month, year=today.year)
  4. lastMonth = first - datetime.timedelta(days=1)

关系转换

几个关系之间的转化

Datetime Object / String / timestamp / time tuple

关系转换例子

datetime <=> string

datetime -> string

  1. import datetime
  2. datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  3. '2015-01-12 23:13:08'

string -> datetime

  1. import datetime
  2. datetime.datetime.strptime("2014-12-31 18:20:10", "%Y-%m-%d %H:%M:%S")
  3. datetime.datetime(2014, 12, 31, 18, 20, 10)
  4. datetime <=> timetuple
  5. datetime -> timetuple
  6. import datetime
  7. datetime.datetime.now().timetuple()
  8. time.struct_time(tm_year=2015, tm_mon=1, tm_mday=12, tm_hour=23, tm_min=17, tm_sec=59, tm_wday=0, tm_yday=12, tm_isdst=-1)
  9. timetuple -> datetime

timetuple => timestamp => datetime [看后面datetime<=>timestamp]

datetime <=> date

datetime -> date

  1. import datetime
  2. datetime.datetime.now().date()
  3. datetime.date(2015, 1, 12)

date -> datetime

  1. datetime.date.today()
  2. datetime.date(2015, 1, 12)
  3. today = datetime.date.today()
  4. datetime.datetime.combine(today, datetime.time())
  5. datetime.datetime(2015, 1, 12, 0, 0)
  6. datetime.datetime.combine(today, datetime.time.min)
  7. datetime.datetime(2015, 1, 12, 0, 0)

datetime <=> timestamp

datetime -> timestamp

  1. now = datetime.datetime.now()
  2. timestamp = time.mktime(now.timetuple())
  3. timestamp
  4. 1421077403.0

timestamp -> datetime

  1. datetime.datetime.fromtimestamp(1421077403.0)
  2. datetime.datetime(2015, 1, 12, 23, 43, 23)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注