如何更改格式以执行日期算术?

时间:2017-04-22 20:01:11

标签: python

我想从2个日期的差异中获得一个整数(天)

end= (pd.datetime.today()-BDay(1)).strftime('%Y-%m-%d')
start= dt.datetime.strptime(4/4/2016, '%m/%d/%Y').strftime('%Y-%m-%d')
在搜索结果中,我找到了这篇文章,所以我尝试了同样的方法 Calculate date time difference python

 difference=end-start
 print(difference.total_days())

TypeError: unsupported operand type(s) for -: 'str' and 'str'

我不知道如何更改此格式是一个日期

1 个答案:

答案 0 :(得分:1)

请勿将日期转换为字符串:

let sorted = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].sort((a,b) => {
  console.log(a,b)
  return a < b ? -1 : (a > b ? 1 : 0)
})
// 0 10
// 0 5
// 10 5
// 2 5
// 3 5
// 4 5
// 1 5
// 6 5
// 9 5
// 8 5
// 7 5
// 0 2
// 2 3
// 3 4
// 4 1
// 3 1
// 2 1
// 0 1
// 6 7
// 7 8
// 8 9
// 9 10

console.log(sorted)
//=> [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]