获得本年开始时间,本月开始时间,本周开始时间,今日开始时间的时间戳
时间: 2023-11-14 07:19:07 浏览: 93
如何获取本周,本月,本日的开始时间和结束时间的时间戳.docx
以下是Python代码实现:
```python
import time
import datetime
# 获取本年开始时间的时间戳
year_start = datetime.datetime.now().replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
year_start_timestamp = int(time.mktime(year_start.timetuple()))
# 获取本月开始时间的时间戳
month_start = datetime.datetime.now().replace(day=1, hour=0, minute=0, second=0, microsecond=0)
month_start_timestamp = int(time.mktime(month_start.timetuple()))
# 获取本周开始时间的时间戳
week_start = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - datetime.timedelta(days=datetime.datetime.now().weekday())
week_start_timestamp = int(time.mktime(week_start.timetuple()))
# 获取今日开始时间的时间戳
today_start = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
today_start_timestamp = int(time.mktime(today_start.timetuple()))
print("本年开始时间戳:", year_start_timestamp)
print("本月开始时间戳:", month_start_timestamp)
print("本周开始时间戳:", week_start_timestamp)
print("今日开始时间戳:", today_start_timestamp)
```
输出结果:
```
本年开始时间戳: 1609430400
本月开始时间戳: 1612137600
本周开始时间戳: 1612780800
今日开始时间戳: 1612828800
```
阅读全文