本文介绍了比较日期庆典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用bash比较两个日期/时间。结果
输入格式:
I need to compare two dates/times using bash.
Input format:
2014-12-01T21:34:03 + 02:00
我想这个格式转换为 INT
,然后比较 INT
S中的两个日期。
I want to convert this format to int
and then compare the int
s of the two dates.
抑或是bash的另一种方式来比较两个日期?
Or does bash have another way to compare two dates?
推荐答案
您可以比较与 [ []]
是这样的:
You can compare lexicographically with the conditional construct [[ ]]
in this way:
[[ "2014-12-01T21:34:03+02:00" < "2014-12-01T21:35:03+02:00" ]]
从man:
[EX pression]结果
根据不同的条件前pression前pression的评价返回0或1状态。
新的更新:
如果您需要使用不同的时区,比较次,可以先转换的时间是这样的:
If you need to compare times with different time-zone, you can first convert those times in this way:
get_date() {
date --utc --date="$1" +"%Y-%m-%d %H:%M:%S"
}
$ get_date "2014-12-01T14:00:00+00:00"
2014-12-01 14:00:00
$ get_date "2014-12-01T12:00:00-05:00"
2014-12-01 17:00:00
$ [[ $(get_date "2014-12-01T14:00:00+00:00") < $(get_date "2014-12-01T12:00:00-05:00") ]] && echo it works
it works
这篇关于比较日期庆典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!