zoukankan      html  css  js  c++  java
  • python(6)时间戳和北京时间互转,输出当前的时间和推到七天前的日期

    项目发展的需要:(包含时间函数)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:Python27python.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
  • 相关阅读:
    【转】CUDA5/CentOS6.4
    【转】centos 6.4 samba 安装配置
    【转】Install MATLAB 2013a on CentOS 6.4 x64 with mode silent
    【转】Getting xrdp to work on CentOS 6.4
    【VLFeat】使用matlab版本计算HOG
    Unofficial Windows Binaries for Python Extension Packages
    March 06th, 2018 Week 10th Tuesday
    March 05th, 2018 Week 10th Monday
    March 04th, 2018 Week 10th Sunday
    March 03rd, 2018 Week 9th Saturday
  • 原文地址:https://www.cnblogs.com/lovychen/p/5226958.html
Copyright © 2011-2022 走看看