如何找到两个日期之间的天数差异? [英] How to find the difference in days between two dates?

查看:31
本文介绍了如何找到两个日期之间的天数差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A="2002-20-10"
B="2003-22-11"

A="2002-20-10"
B="2003-22-11"

如何求两个日期相差的天数?

How to find the difference in days between two dates?

推荐答案

如果你有 GNU date,它允许打印任意日期的表示 (-d选项).在这种情况下,将日期转换为自 EPOCH 以来的秒数,减去并除以 24*3600.

If you have GNU date, it allows to print the representation of an arbitrary date (-d option). In this case convert the dates to seconds since EPOCH, subtract and divide by 24*3600.

使用 GNU 日期的示例(来自 https://stackoverflow.com/a/9008871/215713):>

Example using GNU date (from https://stackoverflow.com/a/9008871/215713):

let DIFF=($(date +%s -d 20210131)-$(date +%s -d 20210101))/86400
echo $DIFF
30

这也适用于其他格式的日期,例如2021-01-31".

This also works with dates in other formats, for example "2021-01-31".

其他答案建议了不需要 GNU 日期的方法.

Other answers suggest ways to do it that don't require GNU date.

这篇关于如何找到两个日期之间的天数差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆