比较日期庆典 [英] Comparing dates bash

查看:117
本文介绍了比较日期庆典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用bash比较两个日期/时间。结果
输入格式:

I need to compare two dates/times using bash.
Input format:

2014-12-01T21:34:03 + 02:00

2014-12-01T21:34:03+02:00

我想这个格式转换为 INT ,然后比较 INT S中的两个日期。

I want to convert this format to int and then compare the ints 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状态。

[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression.

新的更新:

如果您需要使用不同的时区,比较次,可以先转换的时间是这样的:

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

这篇关于比较日期庆典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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