Python DateTime 时间日期处理.
DateTime
获取当前 DateTime
1 | from datetime timport datetime |
获取当天 date
1 | datetime.date.today() |
获取明天/前N天
1 | # 明天 |
获取当天开始和结束时间
1 | datetime.datetime.combine(datetime.date.today(), datetime.time.min) |
获取两个 datetime 的时间差
1 | now = datetime.now() |
获取本周/本月/上月最后一天
- 本周最后一天
1 | import datetime |
- 本月最后一天
1 | import calendar |
- 上月最后一天
1 | import datetime |
转换
datetime <=> string
- datetime -> string
1 | import datetime |
- string -> datetime
1 | import datetime |
datetime <=> timetuple
- datetime -> timetuple
1 | import datetime |
- timetuple -> datetime
1 | timetuple => timestamp => datetime [看后面datetime<=>timestamp] |
datetime <=> date
- datetime -> date
1 | import datetime |
- date -> datetime
1 | datetime.date.today() |
datetime <=> timestamp
- datetime -> timestamp
1 | now = datetime.datetime.now() |
timestamp -> datetime
1 | datetime.datetime.fromtimestamp(1421077403.0) |
Via