Search code examples
unixaix

Date Argument format conversion from YYYYMMDD to DD-MON-YYYY


Can anyone let me know how to convert the date argument value YYYYMMDD(i.e. For ex- 20171010) to the date format 'DD-MON-YYYY'(i.e. 10-OCT-2017). Also i need to validate the date which user specifies as argument while executing the script.

Note: File System will only supports AIX commands. I have got suggestion like below which doesn't supported by AIX File system.

Refer the page - https://www.unix.com/shell-programming-and-scripting/173248-converting-date-yyyymmdd-dd-mon-yyyy.html

https://www.unix.com/shell-programming-and-scripting/58675-change-date-dd-mmm-yyyy-mm-dd-yyyy.html

date -f date -j date -d


Solution

  • St_dt="$2"
    St_D="${St_dt#??????}"
    tmp="${St_dt%??}"
    St_M="${tmp#????}"
    if [ $St_M -le 0 -o $St_M -gt 12 ];
    then
    echo "$St_M is invalid month."
    exit 1
    fi
    St_Y="${tmp%??}"
    case "$St_M" in
    01) St_M=JAN ;;
    02) St_M=FEB ;;
    03) St_M=MAR ;;
    04) St_M=APR ;;
    05) St_M=MAY ;;
    06) St_M=JUN ;;
    07) St_M=JUL ;;
    08) St_M=AUG ;;
    09) St_M=SEP ;;
    10) St_M=OCT ;;
    11) St_M=NOV ;;
    12) St_M=DEC ;;
    esac
    Start_dt="${St_D}-${St_M}-${St_Y}"
    echo "$Start_dt"