Linux 在 bash 中转换日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6508819/
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-05 04:48:08  来源:igfitidea点击:

Convert date formats in bash

linuxbashdateawk

提问by vehomzzz

I have a date in this format: "27 JUN 2011" and I want to convert it to 20110627

我有一个这种格式的日期:“27 JUN 2011”,我想将它转换为 20110627

Is it possible to do in bash?

可以用 bash 做吗?

采纳答案by matchew

#since this was yesterday
date -dyesterday +%Y%m%d

#more precise, and more recommended
date -d'27 JUN 2011' +%Y%m%d

#assuming this is similar to yesterdays `date` question from you 
#http://stackoverflow.com/q/6497525/638649
date -d'last-monday' +%Y%m%d

#going on @seth's comment you could do this
DATE="27 jun 2011"; date -d"$DATE" +%Y%m%d

#or a method to read it from stdin
read -p "  Get date >> " DATE; printf "  AS YYYYMMDD format >> %s"  `date
-d"$DATE" +%Y%m%d`    

#which then outputs the following:
#Get date >> 27 june 2011   
#AS YYYYMMDD format >> 20110627

#if you really want to use awk
echo "27 june 2011" | awk '{print "date -d\""FSFS"\" +%Y%m%d"}' | bash

#note | bash just redirects awk's output to the shell to be executed
#FS is field separator, in this case you can use 
date -d "25 JUN 2011" +%Y%m%d
to print the line #But this is useful if you have more than one date on a line

More on Dates

更多关于日期

note this only works on GNU date

请注意,这仅适用于 GNU 日期

I have read that:

我读过:

Solaris version of date, which is unable to support -dcan be resolve with replacing sunfreeware.com version of date

Solaris版本的date,无法支持-d可以通过更换sunfreeware.com版本的date解决

回答by Seth Robertson

20110625

outputs

产出

convert_date () {
    local months=( JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC )
    local i
    for (( i=0; i<11; i++ )); do
        [[  = ${months[$i]} ]] && break
    done
    printf "%4d%02d%02d\n"  $(( i+1 )) 
}

回答by glenn Hymanman

Just with bash:

只需使用 bash:

d=$( convert_date 27 JUN 2011 )

And invoke it like this

并像这样调用它

d_old="27 JUN 2011"
d=$( convert_date $d_old )  # not quoted

Or if the "old" date string is stored in a variable

或者如果“旧”日期字符串存储在变量中

$ date +"%Y%m%d"
20150330

回答by pjammer

Maybe something changed since 2011 but this worked for me:

也许自 2011 年以来发生了一些变化,但这对我有用:

$ date -j -f "%m/%d/%y %H:%M:%S %p" "8/22/15 8:15:00 am" +"%m%d%y"
082215

No need for the -dto get the same appearing result.

无需-d获得相同的出现结果。

回答by DustinB

On OSX, I'm using -f to specify the input format, -j to not attempt to set any date, and an output format specifier. For example:

在 OSX 上,我使用 -f 来指定输入格式,使用 -j 不尝试设置任何日期,以及一个输出格式说明符。例如:

$ date -j -f "%d %b %Y" "27 JUN 2011" +%Y%m%d
20110627

Your example:

你的例子:

#
# Convert one date format to another
# 
# Usage: convert_date_format <input_format> <date> <output_format>
#
# Example: convert_date_format '%b %d %T %Y %Z' 'Dec 10 17:30:05 2017 GMT' '%Y-%m-%d'
convert_date_format() {
  local INPUT_FORMAT=""
  local INPUT_DATE=""
  local OUTPUT_FORMAT=""
  local UNAME=$(uname)

  if [[ "$UNAME" == "Darwin" ]]; then
    # Mac OS X
    date -j -f "$INPUT_FORMAT" "$INPUT_DATE" +"$OUTPUT_FORMAT"
  elif [[ "$UNAME" == "Linux" ]]; then
    # Linux
    date -d "$INPUT_DATE" +"$OUTPUT_FORMAT"
  else
    # Unsupported system
    echo "Unsupported system"
  fi
}

# Example: 'Dec 10 17:30:05 2017 GMT' => '2017-12-10'
convert_date_format '%b %d %T %Y %Z' 'Dec 10 17:30:05 2017 GMT' '%Y-%m-%d'

回答by David Granqvist

If you would like a bash function that works both on Mac OS X and Linux:

如果您想要一个同时适用于 Mac OS X 和 Linux 的 bash 函数:

data=`date`
datatime=`date -d "${data}" '+%Y%m%d'`
echo $datatime
20190206

回答by Matt Vegas

It's enough to do:

这样做就足够了:

data=`date`
datatime=`date -d "${data}" '+%Y%m%d %T'`

echo $data
Wed Feb 6 03:57:15 EST 2019

echo $datatime
20190206 03:57:15

If you want to add also the time you can use in that way

如果您还想添加可以以这种方式使用的时间

##代码##