Python中常用时间日期

tech2023-01-21  102

python spyder 常用快捷键 备忘

选中多行后:

Ctrl + 1: 注释/反注释

Ctrl + 4/5: 块注释/块反注释

Ctrl + L: 跳转到行号

Tab/Shift + Tab: 代码缩进/反缩进

 

常用时间处理方法 备忘

# -*- coding: utf-8 -*- """ Created on Thu Sep 3 13:26:17 2020 @author: Jeason """ import datetime import time print('今天日期:') today = datetime.date.today() print(' ',today) print('一天前日期:') yesterday = today - datetime.timedelta(days=1) print(' ',yesterday) print('上个月日期:') last_month = today.month - 1 if today.month - 1 else 12 print(' ',last_month) print('当前时间戳:') time_stamp = time.time() print(' ',time_stamp) print('时间戳转datetime:') a = datetime.datetime.fromtimestamp(time_stamp) print(' ',a) print('datetime转时间戳:') b = int(time.mktime(today.timetuple())) print(' ',b) print('datetime转字符串:') today_str = today.strftime("%Y-%m-%d") print(' ',today_str) print('字符串转datetime:') today = datetime.datetime.strptime(today_str, "%Y-%m-%d") print(' ',today) print('补时差:') c = today + datetime.timedelta(hours=8) print(' ',c)

输出:

今天日期: 2020-09-03 一天前日期: 2020-09-02 上个月日期: 8 当前时间戳: 1599111659.1655507 时间戳转datetime: 2020-09-03 13:40:59.165551 datetime转时间戳: 1599062400 datetime转字符串: 2020-09-03 字符串转datetime: 2020-09-03 00:00:00 补时差: 2020-09-03 08:00:00

 

最新回复(0)