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 老司机还是刚入门 Python 不久,你一定遇到过UnicodeEncodeError、UnicodeDecodeError 错误,每当遇到错误我们...

django的model操作汇整详解

django的model操作汇整详解

单表操作 增加数据 auther_obj = {"auther_name":"崔皓然","auther_age":1} models.auther.objects.create(...

Python编程之变量赋值操作实例分析

本文实例讲述了Python编程之变量赋值操作。分享给大家供大家参考,具体如下: #coding=utf8 ''''' Python中主要通过等号(=)进行赋值。 Python中的赋值...

在Linux系统上通过uWSGI配置Nginx+Python环境的教程

1.安装ubuntu有uwsgi的ppa: add-apt-repository ppa:stevecrozz/ppa apt-get update apt-get instal...

Flask框架的学习指南之制作简单blog系统

Flask框架的学习指南之制作简单blog系统

之前写了一篇flask开发环境搭建,今天继续,进行一个实战小项目-blog系统。 blog系统很简单,只有一个页面,然后麻雀虽小五脏俱全。这里目的不是为了做项目而做项目,这篇文章本意是通...