项目发展的需要:(包含时间函数)time datetime
时间戳和北京时间互转
1 import time2 import datetime3 s = '2015-04-17 11:25:30'4 d = datetime.datetime.strptime(s,"%Y-%m-%d %H:%M:%S")5 print int(time.mktime(d.timetuple()))
运行结果:1429241130
需要当前的日期,并显示出时间轴,然后推出七天前的具体日期
1 #! /usr/bin/env python 2 # -*- coding=utf-8 -*- 3 import re 4 import time 5 from datetime import datetime 6 now = datetime.now() 7 list = [0,31,28,31,30,31,30,31,31,30,31,30,31] 8 def judge(year):#判断是否是闰年 9 if year % 100 == 0 and year % 400 == 0:10 return 111 elif year % 100 != 0 and year % 4 == 0:12 return 113 return 014 15 def GetNowTime():16 #当前的时间17 print "Now Time:"18 print time.time()19 print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))20 # 输出当前的:年月日 承接上面的 now = datetime.now()21 # y = now.year22 # m = now.month23 # d = now.day24 #输入年月日:25 print "input:"26 y = int (raw_input())27 m = int (raw_input())28 d = int (raw_input())29 # print time.time()30 # 输出年月日+具体的时间31 # y = now.year32 # m = now.month33 # d = now.day34 ymd = judge(y)35 print "Input Time:"36 print str(y)+"-"+str(m)+"-"+str(d)37 #如果是闰年,则二月份 要加一38 list[2] = list[2] + ymd39 #时间倒退7天40 if(d>7):41 d = d-742 else:43 #if it not is Janurary44 if m!=1:45 m = m-146 d = list[m]+d-747 #if it is Janurary48 else:49 y = y -1 #年份减去150 m = 12 #月份到12月51 d = d+list[12]-752 #恢复二月的原始天数53 print "eryue: " +str(list [2])54 list[2] = list[2] - ymd55 #输出七天七的日期56 print 'Seven days ago:'57 print str(y)+"-"+str(m)+"-"+str(d)58 if __name__ == "__main__":59 GetNowTime()
代码测试:
D:\Python27\python.exe D:/py/Bfun/donghua/test.pyNow Time:1456717147.792016-02-29 11:39:07 input:20150307Input Time:2015-3-7eryue: 28Seven days ago:2015-2-28
input:20160307Input Time:2016-3-7eryue: 29Seven days ago:2016-2-29 input: 2016 01 07 Input Time: 2016-1-7 eryue: 29 Seven days ago: 2015-12-31