python和shell计算两天相隔多少天

python

import datetime
import time
if __name__ == '__main__':
    start_date = "2017-12-14"
    end_date = "2017-12-21"
    tmp = time.strptime(start_date, "%Y-%m-%d")
    tmp1 = time.strptime(end_date, "%Y-%m-%d")
    begin = datetime.date(tmp[0], tmp[1], tmp[2])
    end = datetime.date(tmp1[0], tmp1[1], tmp1[2])
    count = 0
    for i in range((end - begin).days):
        count+=1
    print(count)

 

 

shell

#/bin/bash

start_date='2017-12-14'
end_date='2017-12-21'

test_date="2017-12-14 12:12:12"
test_date=($test_date +%Y-%m-%d)
echo $test_date

#@step{desc:"计算两天之间间隔的秒数,并计算相隔的天数"}
s_count=$(($(date +%s -d "${end_date}")-$(date +%s -d "${start_date}")))
d_count=$(($s_count/86400))
echo $d_count

 

posted @ 2017-12-22 16:14  止水、三千  阅读(550)  评论(0)    收藏  举报