如何使用 UNIX 命令比较两个日期 [英] How to compare two dates using UNIX commands

查看:27
本文介绍了如何使用 UNIX 命令比较两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的约会

Date1="2012 年 10 月 21 日星期三 13:05:14"

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

Date2="2010 年 11 月 21 日星期三 12:55:30"

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

Date3="2012 年 11 月 21 日星期三 14:22:30"

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

我如何知道最新日期?

推荐答案

unix date 命令可以转换日期格式,参见此处.所以在 bash 中,你会做这样的事情:

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}"

这篇关于如何使用 UNIX 命令比较两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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