python获取各时段起始时间和各种类型时间的相互转换
In [1]: import datetime
In [2]: from datetime import timedelta
1.获取今天时间
In [3]: now = datetime.datetime.now()
In [4]: now
Out[4]: datetime.datetime(2017, 9, 14, 9, 38, 44, 878801)
2.获取明天,昨天的时间
In [5]: tomorrow = now + timedelta(days=1)
In [6]: tomorrow
Out[6]: datetime.datetime(2017, 9, 15, 9, 38, 44, 878801)
In [7]: yesterday = now - timedelta(days=1)
In [8]: yesterday
Out[8]: datetime.datetime(2017, 9, 13, 9, 38, 44, 878801)
3.获取本月时间
In [9]: this_month_start = datetime.datetime(now.year, now.month, 1)
In [10]: this_month_start
Out[10]: datetime.datetime(2017, 9, 1, 0, 0)
In [11]: this_month_end = datetime.datetime(now.year, now.month+1, 1) - timedelta(days=1)
In [12]: this_month_end
Out[12]: datetime.datetime(2017, 9, 30, 0, 0)
4.获取上月时间
In [13]: last_month_end = this_month_start - timedelta(days=1)
In [14]: last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1)
In [15]: last_month_start
Out[15]: datetime.datetime(2017, 8, 1, 0, 0)
In [16]: last_month_end
Out[16]: datetime.datetime(2017, 8, 31, 0, 0)
5.获取近30天时间
In [17]: last_thirty_days = now - timedelta(days=30)
In [18]: last_thirty_days
Out[18]: datetime.datetime(2017, 8, 15, 9, 38, 44, 878801)
6.获取本季起始时间(可通过本季起始时间获取上季开始时间,从而获取上季起始时间)
In [19]: m = (now.month -1) - (now.month - 1)%3 +1
In [20]: m
Out[20]: 7
In [21]: this_quarter_satart = datetime.datetime(now.year, m, 1)
In [23]: this_quarter_end = datetime.datetime(now.year, m+3, 1) - timedelta(days=1)
In [24]: this_month_start
Out[24]: datetime.datetime(2017, 9, 1, 0, 0)
In [25]: this_month_end
Out[25]: datetime.datetime(2017, 9, 30, 0, 0)
In [26]: this_quarter_satart
Out[26]: datetime.datetime(2017, 7, 1, 0, 0)
In [27]: this_quarter_end
Out[27]: datetime.datetime(2017, 9, 30, 0, 0)
7.本周起始时间
In [29]: this_week_start = now - timedelta(days=now.weekday())
In [30]: this_week_end = now + timedelta(days=6-now.weekday())
In [31]: this_week_start
Out[31]: datetime.datetime(2017, 9, 11, 9, 38, 44, 878801)
In [32]: this_week_end
Out[32]: datetime.datetime(2017, 9, 17, 9, 38, 44, 878801)
8.上周起始时间
In [33]: last_week_start = now - timedelta(days=now.weekday()+7)
In [34]: last_week_end = now - timedelta(days=now.weekday()+1)
In [35]: last_week_start
Out[35]: datetime.datetime(2017, 9, 4, 9, 38, 44, 878801)
In [36]: last_week_end
Out[36]: datetime.datetime(2017, 9, 10, 9, 38, 44, 878801)
9.字符串时间和datetime的相互转换
In [37]: str_time = now.strftime("%Y-%m-%d %H:%m:%s")
In [38]: str_time
Out[38]: '2017-09-14 09:09:1505396324'
In [41]: dt = datetime.datetime.strptime('2017-09-14',"%Y-%m-%d")
In [42]: dt
Out[42]: datetime.datetime(2017, 9, 14, 0, 0)
10.date转换为时间戳
In [16]: dt = datetime.date(2017, 8, 8)
In [17]: dt
Out[17]: datetime.date(2017, 8, 8)
In [18]: timestamp = time.mktime(dt.timetuple())
In [19]: timestamp
Out[19]: 1502121600.0
11.
时间戳到--字符串时间
datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
字符串到时间戳
s = time.mktime(time.strptime('2016-06-06 10:00:00','%Y-%m-%d %H:%M:%S'))