项目发展的需要:(包含时间函数)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)时间戳和北京时间互转,输出当前的时间和推到七天前的日期的更多相关文章

  1. [转载]vb 时间戳与时间互转

    转自:https://blog.csdn.net/boys1999/article/details/23298415 vb 时间戳与时间互转 2014年04月09日 21:13:47 boys1999 ...

  2. Python 将时间戳转换为本地时间并进行格式化

    在python中,时间戳默认是为格林威治时间,而我们为东八区 使用localtime() 本地化时间戳 使用 strftime() 格式化时间戳 time = time.strftime('%Y%m% ...

  3. python学习之老男孩python全栈第九期_day019知识点总结——collections模块、时间模块、random模块、os模块、sys模块

    一. collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:namedtuple.deque.Counte ...

  4. Python 获取时间戳

    Python 获取时间通过 time 模块 如下代码,是通过获取当前的时间,按照格式输出 Python默认获取当前的时间返回的都是时间的元组,下面是元组的,字符串时间的一个转换输出 # -*- cod ...

  5. 使用python制作时间戳转换工具

    使用python制作时间戳转换工具 python 时间戳转日期 日期转时间戳 前言:作为一个程序员一般情况下,json和时间戳是常用的两个工具,我咨询过很多个朋友,他们一般都是通过在线工具对json进 ...

  6. Python常用时间操作总结【取得当前时间、时间函数、应用等】转载

    Python常用时间操作总结[取得当前时间.时间函数.应用等] 转载  2017-05-11   作者:清风乐逍遥    我要评论 这篇文章主要介绍了Python常用时间操作,包括取得当前时间.时间函 ...

  7. Java和JavaScript的时间互传

    原创文章,转载请注明:Java和JavaScript的时间互传 By Lucio.Yang 1.从JavaScript到Java JavaScript: function query(){ var s ...

  8. Python使用时间戳

    1.将字符串的时间转换为时间戳 方法: a = "2013-10-10 23:40:00" 将其转换为时间数组 importtime timeArray = time.strpti ...

  9. centos 7.2 同步北京时间 ,多台机器同步时间

    linux 系统没有北京时间,同步的是上海时间 linux 系统有两个时钟:一个是硬件时钟,即BIOS时间:另一个是系统时钟,是linux系统Kernel(内核)时间. 系统开启时,系统会读取硬件时间 ...

随机推荐

  1. Java——网络编程

     // TODO Auto-generated method stub //获取本地主机IP对象 InetAddress ip = InetAddress.getLocalHost(); Syst ...

  2. Notepad++ Emmet安装方法教程

    Notepad++ Emmet安装后出现 unknown exception提示插件无效Python Script Plugin did not accept the script.以下为记录解决方法 ...

  3. HBase 安装过程记录

    http://blog.csdn.net/chenxingzhen001/article/details/7756129 环境: 操作系统Centos 6.4 32-bit 三台节点 ip       ...

  4. Hadoop 安装记录

    第一步:打开/etc 下面的 profile文件,在其中加入环境变量设置的代码 done JAVA_HOME=/home/hadoop/installer/jdk7u65 PATH=$JAVA_HOM ...

  5. 给OCR文字识别软件添加图像的方法

    ABBYY FineReader 12是一款OCR图片文字识别软件,而且强大的它现在还可使用快速扫描窗口中的快速打开.扫描并保存为图像或任务自动化任务,在没有进行预处理和OCR的ABBYY FineR ...

  6. 用一条UPDATE语句交换两列的值

    在SQL UPDATE语句中,"="右侧的值在整个UPDATE语句中都是一致的,所有更新同时发生!因此以下语句将在没有临时变量的情况下交换两列的值: UPDATE table SE ...

  7. 使用Javascript实现跳转页面和打开新窗口的方法

    1.在原来的窗体中直接跳转用 window.location.href="你所要跳转的页面url"; 2.在新窗体中打开页面用: window.open('你所要跳转的页面url' ...

  8. docker nodejs 基本应用

    1. 安装docker 环境 2. nodejs  应用布局 package.json { "name": "docker-centos-hello", &qu ...

  9. ASP.NET MVC2中Controller向View传递数据的三种方式

    转自:http://www.cnblogs.com/zhuqil/archive/2010/08/03/Passing-Data-from-Controllers-to-View.html 在Asp. ...

  10. jsp常用指令

    Jsp包含三个编译指令和七个动作指令. 三个编译指令为:page.include.taglib. 七个动作指令为:jsp:forward.jsp:param.jsp:include.jsp:plugi ...