python(6)时间戳和北京时间互转,输出当前的时间和推到七天前的日期
项目发展的需要:(包含时间函数)time datetime
时间戳和北京时间互转
import time
import datetime
s = '2015-04-17 11:25:30'
d = datetime.datetime.strptime(s,"%Y-%m-%d %H:%M:%S")
print int(time.mktime(d.timetuple()))
运行结果:1429241130
需要当前的日期,并显示出时间轴,然后推出七天前的具体日期
#! /usr/bin/env python
# -*- coding=utf-8 -*-
import re
import time
from datetime import datetime
now = datetime.now()
list = [0,31,28,31,30,31,30,31,31,30,31,30,31]
def judge(year):#判断是否是闰年
if year % 100 == 0 and year % 400 == 0:
return 1
elif year % 100 != 0 and year % 4 == 0:
return 1
return 0 def GetNowTime():
#当前的时间
print "Now Time:"
print time.time()
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
# 输出当前的:年月日 承接上面的 now = datetime.now()
# y = now.year
# m = now.month
# d = now.day
#输入年月日:
print "input:"
y = int (raw_input())
m = int (raw_input())
d = int (raw_input())
# print time.time()
# 输出年月日+具体的时间
# y = now.year
# m = now.month
# d = now.day
ymd = judge(y)
print "Input Time:"
print str(y)+"-"+str(m)+"-"+str(d)
#如果是闰年,则二月份 要加一
list[2] = list[2] + ymd
#时间倒退7天
if(d>7):
d = d-7
else:
#if it not is Janurary
if m!=1:
m = m-1
d = list[m]+d-7
#if it is Janurary
else:
y = y -1 #年份减去1
m = 12 #月份到12月
d = d+list[12]-7
#恢复二月的原始天数
print "eryue: " +str(list [2])
list[2] = list[2] - ymd
#输出七天七的日期
print 'Seven days ago:'
print str(y)+"-"+str(m)+"-"+str(d)
if __name__ == "__main__":
GetNowTime()
代码测试:
D:\Python27\python.exe D:/py/Bfun/donghua/test.py
Now Time:
1456717147.79
2016-02-29 11:39:07 input:
2015
03
07
Input Time:
2015-3-7
eryue: 28
Seven days ago:
2015-2-28
input:
2016
03
07
Input Time:
2016-3-7
eryue: 29
Seven days ago:
2016-2-29 input:
2016
01
07
Input Time:
2016-1-7
eryue: 29
Seven days ago:
2015-12-31
python(6)时间戳和北京时间互转,输出当前的时间和推到七天前的日期的更多相关文章
- [转载]vb 时间戳与时间互转
转自:https://blog.csdn.net/boys1999/article/details/23298415 vb 时间戳与时间互转 2014年04月09日 21:13:47 boys1999 ...
- Python 将时间戳转换为本地时间并进行格式化
在python中,时间戳默认是为格林威治时间,而我们为东八区 使用localtime() 本地化时间戳 使用 strftime() 格式化时间戳 time = time.strftime('%Y%m% ...
- python学习之老男孩python全栈第九期_day019知识点总结——collections模块、时间模块、random模块、os模块、sys模块
一. collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:namedtuple.deque.Counte ...
- Python 获取时间戳
Python 获取时间通过 time 模块 如下代码,是通过获取当前的时间,按照格式输出 Python默认获取当前的时间返回的都是时间的元组,下面是元组的,字符串时间的一个转换输出 # -*- cod ...
- 使用python制作时间戳转换工具
使用python制作时间戳转换工具 python 时间戳转日期 日期转时间戳 前言:作为一个程序员一般情况下,json和时间戳是常用的两个工具,我咨询过很多个朋友,他们一般都是通过在线工具对json进 ...
- Python常用时间操作总结【取得当前时间、时间函数、应用等】转载
Python常用时间操作总结[取得当前时间.时间函数.应用等] 转载 2017-05-11 作者:清风乐逍遥 我要评论 这篇文章主要介绍了Python常用时间操作,包括取得当前时间.时间函 ...
- Java和JavaScript的时间互传
原创文章,转载请注明:Java和JavaScript的时间互传 By Lucio.Yang 1.从JavaScript到Java JavaScript: function query(){ var s ...
- Python使用时间戳
1.将字符串的时间转换为时间戳 方法: a = "2013-10-10 23:40:00" 将其转换为时间数组 importtime timeArray = time.strpti ...
- centos 7.2 同步北京时间 ,多台机器同步时间
linux 系统没有北京时间,同步的是上海时间 linux 系统有两个时钟:一个是硬件时钟,即BIOS时间:另一个是系统时钟,是linux系统Kernel(内核)时间. 系统开启时,系统会读取硬件时间 ...
随机推荐
- FZU-2216 The Longest Straight (二分枚举)
题目大意:给n个0~m之间的数,如果是0,那么0可以变为任意的一个1~m之间的一个数.从中选出若干个数,使构成一个连续的序列.问能构成的最长序列的长度为多少? 题目分析:枚举连续序列的起点,二分枚举二 ...
- Android开发之多级下拉列表菜单实现(仿美团,淘宝等)
注:本文转载于:http://blog.csdn.net/minimicall/article/details/39484493 我们在常用的电商或者旅游APP中,例如美团,手机淘宝等等,都能够看的到 ...
- Lua5.1基本函数库介绍
Lua5.1基本函数库介绍assert (v [, message])功能:相当于C的断言,参数:v:当表达式v为nil或false将触发错误,message:发生错误时返回的信息,默认为" ...
- mysql SQL SERVER 的算法
Filesort Probes http://dev.mysql.com/doc/refman/5.7/en/dba-dtrace-ref-filesort.html http://dev.mysql ...
- SQL Debugging
C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Binn>“C:\Program Files\Debugging ...
- 在CentOS上安装Python
首先我们需要在服务器上安装一个比较新的 Python,CentOS 5.8 默认装的 Python 是 2.4.3. [root@nowamagic ~]# python -V Python 我们需要 ...
- pgbouncer配置
DESCRIPTION pgbouncer is a PostgreSQL connection pooler. Any target application can be connected to ...
- EDIUS如何实现抠图
有时候我们把素材图片导入到EDIUS中会发现图片的背景有点不合适,这时候就会想替换掉这个背景.那么EDIUS也可以像PS一样替换掉背景吗?答案当然是肯定的,现在小编就带领你们一起来学习EDIUS抠图吧 ...
- 011. asp.net内置对象
Response对象: Response代表了服务器响应对象, 主要用于将数据从服务器发送回浏览器; 每次客户端发出一个请求的时候,服务器就会用一个响应对象来处理这个请求,处理完这个请求之后,服务器就 ...
- 一种JavaScript 类的设计模式
一种JavaScript 类的设计模式尽管前面介绍了如何定义一个类,如何初始化一个类的实例,但既可以在function定义的函数体中添加成员,又可以用prototype 定义类的成员,代码显的很混乱, ...