How to compare two dates using UNIX commands [closed]

ⅰ亾dé卋堺 提交于 2019-12-22 16:37:09

问题


I have dates like this

Date1="Wed Oct 21 13:05:14 2012"

Date2="Wed Nov 21 12:55:30 2010"

Date3="Wed Nov 21 14:22:30 2012"

How do I find out the latest date?


回答1:


The unix date command can convert date formats, see for example here. So in bash, you'd do something like this:

#!/bin/sh

Date1="Wed Oct 21 13:05:14 2012"
Date2="Wed Nov 21 12:55:30 2010"
Date3="Wed Nov 21 14:22:30 2012"

ts1=`date -d"${Date1}" +%Y%m%d%H%M%S`
ts2=`date -d"${Date2}" +%Y%m%d%H%M%S`
ts3=`date -d"${Date3}" +%Y%m%d%H%M%S`

latest=ts1

if [ $ts2 -gt $ts1 ]; then
    latest=ts2
fi

if [ $ts3 -gt $ts1 ]; then
    latest=ts3
fi

echo "Latest date is ${latest}"


来源:https://stackoverflow.com/questions/13516000/how-to-compare-two-dates-using-unix-commands

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!