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(内核)时间. 系统开启时,系统会读取硬件时间 ...
随机推荐
- codeForce-19D Points (点更新+离散化)
题目大意:在二维坐标系的x正半轴,y正半轴和第一象限内,有三种操作: 1.add x,y (添加点<x,y>): 2.remove x,y(移除点<x,y>): 3.find ...
- ThinkPHP缓存微信公众号access_token
access_token作为微信的全局票据,在高级接口中需要频繁调用,其有效期为7200秒,即2小时,而微信官方对调用Token的次数有限制(参见接口频率限制说明),获取access_token每日限 ...
- 用 C# 在 Windows 7 中写注册表想到的
摘自:http://blog.163.com/dpj_001/blog/static/2742941520110251500753/ 某日做一个项目,需要在注册表中加入键,同时写值,操作系统环境为 W ...
- C++中this指针的用法详解(转)
原文地址:http://blog.chinaunix.net/uid-21411227-id-1826942.html 1. this指针的用处: 一个对象的this指针并不是对象本身的一部分,不会影 ...
- Android项目——网络图片查看器
效果-=-------------->加入包 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an ...
- C语言调试的几种方法
linux系统下,在不gdb调试的情况下,我们如何解决程序崩溃问题呢?首先想到的就是添加log日志信息,其次还有以下几种方法可以帮助我们分析存在的问题: (一)add2line 程序崩溃时会打出一些崩 ...
- VC++ 0xC0000005: Access violation.
public: COptionDlg(CWnd* pParent = NULL); // 标准构造函数 virtual ~COptionDlg(); TCONFIG m_tCfg; // 对话框数据 ...
- MySQL数据库update更新子查询
比如: ? 1 2 3 4 UPDATE test.tb_vobile a set a.name = '111 ' WHERE a.id = (select max(id) id from test. ...
- 二十种实战调优MySQL性能优化的经验
二十种实战调优MySQL性能优化的经验 发布时间:2012 年 2 月 15 日 发布者: OurMySQL 来源:web大本营 才被阅读:3,354 次 消灭0评论 本文将为大家介 ...
- 非ROOT用户启动Tomcat
[root@Z ~]# adduser tomcat [root@Z ~]# chown -R tomcat:tomcat /usr/local/tomcat/ [root@Z ~]# chown - ...