Python gets methods for timestamps starting yesterday today tomorrow and ending tomorrow

  • 2020-10-31 21:50:58
  • OfStack

As shown below:


#!/usr/bin/python
# coding=utf-8
#
import time
import datetime
#  Today's date 
today = datetime.date.today()
#  Yesterday time 
yesterday = today - datetime.timedelta(days=1)
#  Time tomorrow 
tomorrow = today + datetime.timedelta(days=1)
acquire = today + datetime.timedelta(days=2)
#  The timestamp started yesterday 
yesterday_start_time = int(time.mktime(time.strptime(str(yesterday), '%Y-%m-%d')))
#  The time stamp ended yesterday 
yesterday_end_time = int(time.mktime(time.strptime(str(today), '%Y-%m-%d'))) - 1
#  Time stamps start today 
today_start_time = yesterday_end_time + 1
#  End of day timestamp 
today_end_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d'))) - 1
#  The timestamp starts tomorrow 
tomorrow_start_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d')))
#  End time stamp tomorrow 
tomorrow_end_time = int(time.mktime(time.strptime(str(acquire), '%Y-%m-%d'))) - 1
print ' Today's timestamp '
print today_start_time
print today_end_time
print ' Yesterday's timestamp '
print yesterday_start_time
print yesterday_end_time
print ' Tomorrow time stamp '
print tomorrow_start_time
print tomorrow_end_time

The output


/usr/bin/python2.7 /home/he/dev/python_my/test.py
 Today's timestamp 
1498233600
1498319999
 Yesterday's timestamp 
1498147200
1498233599
 Tomorrow time stamp 
1498320000
1498406399
Process finished with exit code 0

Related articles: