赞
踩
-----------前置条件:
import datetime import time
curr_time = datetime.datetime.now()
输出:
- curr_time = datetime.now()
- timestamp = datetime.strftime(curr_time, '%Y-%m-%d %H:%M:%S')
- print(timestamp)
-
timestamp=curr_time.date()
输出:
timestamp=curr_time.year
输出:
timestamp=curr_time.month
输出:
timestamp=curr_time.day
输出:
timestamp=curr_time.hour
输出:
timestamp=curr_time.minute
输出:
timestamp=curr_time.second
输出:
- t = time.time()
- print(int(t))
输出:
13位
- t = time.time()
- print(int(round(t * 1000)))
输出:
- import time
-
- timestamp = int(time.time())
- last_5min = timestamp - timestamp % 300 #上一个5分钟整的时间戳
- next_5min = timestamp - timestamp % 300 + 300 #下一个5分钟整的时间戳
- print(last_5min, next_5min )
-
- timestamp = int(time.time())
- last_1h = timestamp - timestamp % 3600 #上一个1小时整的时间戳
- next_1h = timestamp - timestamp % 3600 + 3600 #下一个1小时整的时间戳
- print(last_1h, next_1h)
输出:
- import datetime
-
- now=datetime.datetime.now()
- # 前一小时
- a = now - datetime.timedelta(hours=1)
- print("前一小时")
- print(a.strftime("%Y-%m-%d %H:%S:%M")+"\n")
-
- # 前一天
- b = now - datetime.timedelta(days=1)
- print("前一天")
- print(b.strftime("%Y-%m-%d %H:%S:%M")+"\n")
-
- # 上周日
- c = now - datetime.timedelta(days=now.isoweekday())
- print("上周日")
- print(c.strftime("%Y-%m-%d %H:%S:%M")+"\n")
-
- # 上周一
- d = c - datetime.timedelta(days=6)
- print("上周一")
- print(d.strftime("%Y-%m-%d %H:%S:%M")+"\n")
-
- # 上个月最后一天
- e = now - datetime.timedelta(days=now.day)
- print("上个月最后一天")
- print(e.strftime("%Y-%m-%d %H:%S:%M"+"\n"))
-
- # 上个月第一天
- print("上个月第一天")
- print(datetime.datetime(e.year, e.month, 1))
输出:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。