zoukankan      html  css  js  c++  java
  • python时间大小判断,相差天数秒数计算

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    import time,datetime
    import os,sys
    
    #获取并检查外部传入参数
    if(len(sys.argv)==2):
        bizDate=sys.argv[1]
        print "数据日期:",bizDate
        bizDate10 = bizDate[0:4] + '-' + bizDate[4:6] + '-' + bizDate[6:8]
        if len(bizDate) !=8:
           print "业务日期传参非法*********"
    else:
         print "*********传参非法,请输入: YYYYMMDD*********"
         sys.exit(1)
           
    #开始时间
    Start_Time = bizDate10 + ' 13:55:00'
    print '开始时间:  ' + Start_Time
    Start_Time_int = int(time.mktime(time.strptime(Start_Time, "%Y-%m-%d %H:%M:%S")))
    #print Start_Time_int
    
    #当前时间
    Current_Time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print '当前时间:  ' + Current_Time
    Current_Time_int = int(time.mktime(time.strptime(Current_Time, "%Y-%m-%d %H:%M:%S")))
    #print Current_Time_int
    
    #对比
    if Current_Time_int > Start_Time_int:
       print "当前时间大于开始时间"
       #相差多少天
       Days = str((datetime.datetime.fromtimestamp(Current_Time_int)-datetime.datetime.fromtimestamp(Start_Time_int)).days)
       print "当前时间-开始时间相差(天): " + Days
       
       #相差多少秒
       Seconds1 = (datetime.datetime.fromtimestamp(Current_Time_int)-datetime.datetime.fromtimestamp(Start_Time_int)).seconds
       Seconds2 = int(Days)*24*3600
       Seconds = Seconds1 + Seconds2
       print "当前时间-开始时间相差(秒): " + str(Seconds)
       
    elif Current_Time_int == Start_Time_int:
       print "当前时间等于开始时间"
       
    else:
       print "当前时间小于开始时间"
       #相差多少天
       Days = str((datetime.datetime.fromtimestamp(Start_Time_int)-datetime.datetime.fromtimestamp(Current_Time_int)).days)
       print "当前时间-开始时间相差(天): " + Days
       #相差多少秒
       Seconds1 = (datetime.datetime.fromtimestamp(Start_Time_int)-datetime.datetime.fromtimestamp(Current_Time_int)).seconds
       Seconds2 = int(Days)*24*3600
       Seconds = Seconds1 + Seconds2
       print "当前时间-开始时间相差(秒): " + str(Seconds)

    如果:

          当前时间小于开始时间时,可以认为取得是绝对值,相差的天数和秒数。

  • 相关阅读:
    Android:CheckBox控件
    Android:RadioGroup,RadioButton
    Android:ImageView控件显示图片
    Spark:reduceByKey函数的用法
    HIve:beeline终端上在输错hive语句时,无论 Backspace还是delete 都删除不掉错误的语句,没有办法退格
    orchard-1.9.2-1.10.2汉化
    Hive:表1inner join表2结果group by优化
    hive:某张表进行分页
    hive:创建索引
    Android:后台给button绑定onClick事件、当返回项目到手机页面时提示是否退出APP
  • 原文地址:https://www.cnblogs.com/hello-wei/p/13607189.html
Copyright © 2011-2022 走看看