在bash中转换日期格式

我有这样的日期格式:“ 2011年6月27日”,我想将其转换为20110627

可以用bash做吗?


肥皂起泡泡
浏览 880回答 3
3回答

HUWWW

在OSX上,我使用-f来指定输入格式,使用-j来不尝试设置任何日期,并使用输出格式说明符。例如:$ date -j -f "%m/%d/%y %H:%M:%S %p" "8/22/15 8:15:00 am" +"%m%d%y"082215你的例子:$ date -j -f "%d %b %Y" "27 JUN 2011" +%Y%m%d20110627

www说

只用bash:convert_date () {&nbsp; &nbsp; local months=( JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC )&nbsp; &nbsp; local i&nbsp; &nbsp; for (( i=0; i<11; i++ )); do&nbsp; &nbsp; &nbsp; &nbsp; [[ $2 = ${months[$i]} ]] && break&nbsp; &nbsp; done&nbsp; &nbsp; printf "%4d%02d%02d\n" $3 $(( i+1 )) $1}然后像这样调用它d=$( convert_date 27 JUN 2011 )或者,如果“旧”日期字符串存储在变量中d_old="27 JUN 2011"d=$( convert_date $d_old )&nbsp; # not quoted
打开App,查看更多内容
随时随地看视频慕课网APP