【问题标题】:date conversion DD-MMM-YY to YYYYMMDD日期转换 DD-MMM-YY 到 YYYYMMDD
【发布时间】:2011-09-28 16:08:56
【问题描述】:

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

我在 AIX 中,使用 korn shell。

date --datedate -d 在 aix 中都不起作用。

【问题讨论】:

  • 您要转换任意日期吗?还是“现在”?

标签: date ksh aix


【解决方案1】:

纯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

【讨论】:

    【解决方案2】:

    如果您的系统上有tclsh

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

    【讨论】:

      【解决方案3】:

      我在 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");
      }   
      

      【讨论】:

      • 我的缩进被吞了:(
      猜你喜欢
      • 2022-11-10
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多