Linux Bash - 日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14152759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:11:41  来源:igfitidea点击:

Linux Bash - Date Format

linuxbashshelldateformatting

提问by Vince

My date format is yyyy-mm-dd-hh:mm:ss How do I check my input?

我的日期格式是 yyyy-mm-dd-hh:mm:ss 如何检查我的输入?

It should be something like this:

它应该是这样的:

#!/bin/bash

read -p "Date (format yy-mm-dd-HH-MM-SS): " input

check=$(date +"%Y-%m-%d-%H:%M:%S")

if [ $input -eq $check ]; do

     echo "Right!"

else
     echo "False!"

fi

But that doesn't check the date It compares my input with the real date.

但这不会检查日期它将我的输入与实际日期进行比较。

Best Regards Vince

最好的问候文斯

采纳答案by techno

Edited apr 2016!

2016 年 4 月编辑!

See further (stronger method)

进一步查看(更强的方法)

Original post

原帖

Try:

尝试:

#!/bin/bash

read -p "Date (format yyyy-mm-dd): " input
check=$(date +%F)

if [ "$input" == "$check" ]; then
    echo "Right!"
else
    echo "False!"
fi

or

或者

#!/bin/bash

read -p "Date (format YYYY-MN-DD-HH24:MM:SS): " input
check=$(date +%F-%T)

if [ "$input" == "$check" ]; then
    echo "Right!"
else
    echo "False!"
fi

Well tested:

测试良好:

cat >hesdate.sh     # Copy 1st sample and paste to terminal
chmod +x hesdate.sh
date +%F ; ./hesdate.sh
2013-01-04
Date (format yyyy-mm-dd): 2013-01-04
Right!

cat >hesdate.sh     # Copy 2nd sample and paste to terminal
date -d now\ +10\ sec +%F-%T ; ./hesdate.sh 
2013-01-04-10:17:06                                       # copy this line
Date (format YYYY-MN-DD-HH24:MM:SS): 2013-01-04-10:17:06  # past exactly 10 secs after
Right!

Edit add

编辑添加

For testing a date, you could:

要测试日期,您可以:

[[ $input =~ ^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$ ]]

if [[ $input =~ ^2012-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$ ]];then

and/or

和/或

inputSecs=$(date -d "${input%-*} ${input##*-}" +%s)

Using boot method let you confirm formatandreliability of input

使用引导方法让您确认输入的格式可靠性

Stronger method

更强的方法

If you want to check input, there is a finer method:

如果要检查输入,有一个更好的方法:

unset adate
declare -A adate
date=2013-12-04-10:17:06
for field in s:0-59 m:0-59 h-0-23 D-1-31 M-1-12 Y#2000-2100 ;do
   sep=${field:1:1} min=${field:2} field=${field:0:1} max=${min#*-} min=${min%-*}
   crt=${date##*${sep:-#}}
   ((min<=10#$crt&&10#$crt<=max)) && adate[$field]=$crt ||
       echo Error: $crt not between $min and $max in $field field.
   date=${date%$sep*}
 done
declare -p adate

This will dump adatearray variable:

这将转储adate数组变量:

declare -A adate='([D]="04" [M]="12" [Y]="2013" [h]="10" [m]="17" [s]="06" )'

From there, you could re-validate day number:

从那里,您可以重新验证天数

max=$(date -d "${adate[Y]}-${adate[M]}-1 +1 month -1 day" +%d)
((10#${adate[D]}>max)) && echo "Error Day number too high: (${adate[D]}>$max)."

The only thing not tested there is field length if

唯一没有测试的是字段长度,如果

date=2012-02-29-10:17:06

will work, then

会工作,然后

date=2012-2-29-10:17:06

will work too (there is only one digit in day field).

也可以工作(天字段中只有一位数字)。

If needed, you could change the line:

如果需要,您可以更改该行:

for field in s:0-59 m:0-59 h-0-23 D-1-31 M-1-12 Y#2000-2100 ;do
sep=${field:1:1} min=${field:2} field=${field:0:1} max=${min#*-} min=${min%-*}
crt=${date##*${sep:-#}}

for

为了

for field in s:20-59 m:20-59 h-20-23 D-21-31 M-21-12 Y#42000-2100 ;do
sep=${field:1:1} len=${field:2:1} min=${field:3} field=${field:0:1} max=${min#*-} min=${min%-*}
crt=${date##*${sep:-#}}
[ ${#crt} -eq $len ] || echo "Error: Field $field is no $len len: ${#crt}."

Nota: Year field is arbitrarily limited between 2000 and 2100, but this is easy to understand/change.

注意:Year 字段在 2000 和 2100 之间任意限制,但这很容易理解/更改。

回答by wallyk

Change the date prompt like this:

像这样更改日期提示:

read -p "Date (format yyyy-mm-dd): " input

Also, format the date with 4 digit year, and seconds since the minute with

此外,用 4 位年份和从分钟开始的秒数格式化日期

check=$(date +"%Y-%m-%d %H:%M:%S")

回答by JTextor

You can use an inline AWK script:

您可以使用内联 AWK 脚本:

#!/bin/bash

read -p "Date (format yy-mm-dd-HH-MM-SS): " input

echo $input | awk -F"[-:]" '{
    if( NF == 6 &&  >= 0 &&  >= 1 &&  <= 12 &&  >= 1 &&  <= 31 &&  >= 1 &&  <= 24 &&  >= 1 &&  <= 60 &&  >= 1 &&  <= 60 ){
        print "Ok!\n";
    } else {
        print "False!\n";
    }
}'