項目發展的需要:(包含時間函數)time datetime
時間戳和北京時間互轉
1 import time 2 import datetime 3 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 1 11 elif year % 100 != 0 and year % 4 == 0: 12 return 1 13 return 0 14 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.year 22 # m = now.month 23 # d = now.day 24 #輸入年月日: 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.year 32 # m = now.month 33 # d = now.day 34 ymd = judge(y) 35 print "Input Time:" 36 print str(y)+"-"+str(m)+"-"+str(d) 37 #如果是閏年,則二月份 要加一 38 list[2] = list[2] + ymd 39 #時間倒退7天 40 if(d>7): 41 d = d-7 42 else: 43 #if it not is Janurary 44 if m!=1: 45 m = m-1 46 d = list[m]+d-7 47 #if it is Janurary 48 else: 49 y = y -1 #年份減去1 50 m = 12 #月份到12月 51 d = d+list[12]-7 52 #恢復二月的原始天數 53 print "eryue: " +str(list [2]) 54 list[2] = list[2] - ymd 55 #輸出七天七的日期 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.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