Python获取昨天、今天、明天开始、结束时间戳的方法

yipeiwu_com5年前Python基础

如下所示:

#!/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

输出结果

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

以上这篇Python获取昨天、今天、明天开始、结束时间戳的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python字符串对象实现原理详解

Python字符串对象实现原理详解

在Python世界中将对象分为两种:一种是定长对象,比如整数,整数对象定义的时候就能确定它所占用的内存空间大小,另一种是变长对象,在对象定义时并不知道是多少,比如:str,list, s...

Python简单遍历字典及删除元素的方法

本文实例讲述了Python简单遍历字典及删除元素的方法。分享给大家供大家参考,具体如下: 这种方式是一定有问题的: d = {'a':1, 'b':2, 'c':3} for key...

pycharm恢复默认设置或者是替换pycharm的解释器实例

pycharm恢复默认设置或者是替换pycharm的解释器实例

Windows 找到下面的路径,然后删掉即可 # Windows Vista, 7, 8, 10:<SYSTEM DRIVE>\Users\<USER ACCOUN...

浅谈python for循环的巧妙运用(迭代、列表生成式)

介绍 我们可以通过for循环来迭代list、tuple、dict、set、字符串,dict比较特殊dict的存储不是连续的,所以迭代(遍历)出来的值的顺序也会发生变化。 迭代(遍历)...

python实现n个数中选出m个数的方法

python实现n个数中选出m个数的方法

题目: 某页纸上有一个数列A,A包含了按照从小到大的顺序排列的多个自然数,但是因为一些原因,其中有M个连续的位置看不清了。这M个数左边最小的数是X,右边最大的数是Y,这些数之和大于等于P...