Home > Backend Development > Python Tutorial > How to get the start and end timestamps of yesterday, today, and tomorrow in Python

How to get the start and end timestamps of yesterday, today, and tomorrow in Python

不言
Release: 2018-06-02 14:56:40
Original
2238 people have browsed it

This article mainly introduces the method of Python to obtain the start and end timestamps of yesterday, today, and tomorrow. It has certain reference value. Now I share it with you. Friends in need can refer to it

is as follows:

#!/usr/bin/python
# coding=utf-8
#
import time
import datetime
# 今天日期
today = datetime.date.today()
# 昨天时间
yesterday = today - datetime.timedelta(days=1)
# 明天时间
tomorrow = today + datetime.timedelta(days=1)
acquire = today + datetime.timedelta(days=2)
# 昨天开始时间戳
yesterday_start_time = int(time.mktime(time.strptime(str(yesterday), '%Y-%m-%d')))
# 昨天结束时间戳
yesterday_end_time = int(time.mktime(time.strptime(str(today), '%Y-%m-%d'))) - 1
# 今天开始时间戳
today_start_time = yesterday_end_time + 1
# 今天结束时间戳
today_end_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d'))) - 1
# 明天开始时间戳
tomorrow_start_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d')))
# 明天结束时间戳
tomorrow_end_time = int(time.mktime(time.strptime(str(acquire), '%Y-%m-%d'))) - 1
print '今天时间戳'
print today_start_time
print today_end_time
print '昨天时间戳'
print yesterday_start_time
print yesterday_end_time
print '明天时间戳'
print tomorrow_start_time
print tomorrow_end_time
Copy after login

##Output result

/usr/bin/python2.7 /home/he/dev/python_my/test.py
今天时间戳
1498233600
1498319999
昨天时间戳
1498147200
1498233599
明天时间戳
1498320000
1498406399
Process finished with exit code 0
Copy after login

The above is the detailed content of How to get the start and end timestamps of yesterday, today, and tomorrow in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template