日期转换DD-MMM-YY到YYYYMMDD

时间:2011-07-05 09:47:47

标签: date ksh aix

如何将DD-MMM-YY转换为YYYYMMDD

我在AIX中,使用korn shell。

date --datedate -d都不适用于aix。

3 个答案:

答案 0 :(得分:2)

纯粹的ksh:

convert_date () {
  typeset -l date=$1
  typeset IFS="-"
  set -- $date  # now $1 is the day, $2 is the lower-case month, $3 is the year

  typeset months
  set -A months "" jan feb mar apr may jun jul aug sep oct nov dec
  typeset -i m=1

  while [[ $m -le 12 ]]; do 
    if [[ "$2" = "${months[$m]}" ]]; then 
      break
    else
      m=$(( m+1 ))
    fi
  done

  # assume this century
  printf "20%02d%02d%02d\n" "$3" $m "$1"
}

convert_date 06-JUL-11   # ==> 20110706

答案 1 :(得分:0)

如果您的系统中有tclsh

old="06-JUL-11"
new=$( echo "puts [clock format [clock scan $old] -format %Y%m%d]" | tclsh )
echo $new   # ==> 20110706

答案 2 :(得分:0)

我用perl,艰难的方式做到了

sub formatDate_YYYYMMDD 
{
my $date=shift;
my ($day,$mon,$yr) = split /\-/,$date;
my @months=qw(JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC );
$i=0;
foreach $_ (@months)
    {
        $i++; 
        if (/$mon/i){$mon=$i;}
    }
$year=2000+$yr;
$mon=sprintf "%02d",$mon;
print "Invalid month format $date present in $file_name" if $mon ==0;
return("$year$mon$day");
}   
相关问题