项目发展的需要:(包含时间函数)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. Android——GridView

    layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  2. /usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h:No such file or directory的解决办法

    在编译32位HDecode时出现如题所示的错误,原因时没有安装32位glibc库导致的: ubuntu: sudo apt-get install libc6-dev-i386 CentOS:yum ...

  3. 转载:javascript 拖拽排序,简洁示例备忘

    转载自:http://blog.csdn.net/wang4978/article/details/6721157 <html> <head> <title>拖动行 ...

  4. 设置EDIUS字幕时有哪些要注意的

    我们在用EDIUS添加字幕,有时候可能会遇到以下麻烦.例如有的字体在EDIUS中找不到,诗歌的排版问题还有怎么给字幕加光效等等.今天小编主要来给大家解决这三个问题,让你们知道EDIUS字幕设置时应该注 ...

  5. 【转】ASP.NET中服务器控件Table动态生成表格及其属性介绍

    下文所有内容转自开源中国:http://www.oschina.net/question/565065_86453#tags_nav ================================= ...

  6. Openjudge计算概论——数组逆序重放【递归练习】

    /*===================================== 数组逆序重放 总时间限制:1000ms 内存限制:65536kB 描述 将一个数组中的值按逆序重新存放. 例如,原来的顺 ...

  7. .net 开源 JavaScript 解析引擎

    1. Javascript .NET 地址为:http://javascriptdotnet.codeplex.com/ 使用方法: Quick Start This section provides ...

  8. 【转】Win7注册表的使用(更新中)

    一.注册表的存储结构和数据类型 1.基本概念: Windows 7的注册表主要由“键”和“键值”构成,称HKEY为根键(RootKey),SubKey为子键. 键(Key):“位于左侧窗格如同文件夹图 ...

  9. MvcPager2.0 中分页初始化失败的问题

    页面初始化时只有一页数据,或没数据时,出现分页控件初始化失败以及后续Ajax分页功能失效的问题, <div class="pagin"> <div class=& ...

  10. JQuery Uplodify上传附件(同一个页面多个uplodify控件解决方案)

    功能描述:实现同一页面中多个不同附件的上传保存,且做到最大程度的减少代码修改量(最大程度的公用),为了方便实现垃圾数据和垃圾文件的处理,项目采用临时文件夹的方式:即:文件自动上传先保存到临时文件夹下, ...