zoukankan      html  css  js  c++  java
  • [时间序列处理]python中计算日期差

    参考自:http://blog.csdn.net/sinat_37487842/article/details/72864462

     1 import time  
     2 import datetime  
     3   
     4 #计算两个日期相差天数,自定义函数名,和两个日期的变量名。  
     5 def Caltime(date1,date2):  
     6     #%Y-%m-%d为日期格式,其中的-可以用其他代替或者不写,但是要统一,同理后面的时分秒也一样;可以只计算日期,不计算时间。  
     7     #date1=time.strptime(date1,"%Y-%m-%d %H:%M:%S")   
     8     #date2=time.strptime(date2,"%Y-%m-%d %H:%M:%S")  
     9     date1=time.strptime(date1,"%Y-%m-%d")  
    10     date2=time.strptime(date2,"%Y-%m-%d")  
    11     #根据上面需要计算日期还是日期时间,来确定需要几个数组段。下标0表示年,小标1表示月,依次类推...  
    12     #date1=datetime.datetime(date1[0],date1[1],date1[2],date1[3],date1[4],date1[5])  
    13     #date2=datetime.datetime(date2[0],date2[1],date2[2],date2[3],date2[4],date2[5])  
    14     date1=datetime.datetime(date1[0],date1[1],date1[2])  
    15     date2=datetime.datetime(date2[0],date2[1],date2[2])  
    16     #返回两个变量相差的值,就是相差天数  
    17     return date2-date1
  • 相关阅读:
    176. Second Highest Salary
    175. Combine Two Tables
    172. Factorial Trailing Zeroes
    171. Excel Sheet Column Number
    169. Majority Element
    168. Excel Sheet Column Title
    167. Two Sum II
    160. Intersection of Two Linked Lists
    个人博客记录
    <meta>标签
  • 原文地址:https://www.cnblogs.com/imageSet/p/7929569.html
Copyright © 2011-2022 走看看