Date variable in shell script

Hello guys n girls,

I am using ksh script on AIX platform.
I am getting a DATE from input file in "YYYYMMDDHHMISS" format.
Now I want the same date in "YYYYMMDD" format into another variable.

For example,
if the DATE is 20101018121445
then I need 20101018 in another variable.

Please let me know the command to do it.
Thanks in advance.

On solaris on command line below date command gives 20101018, on AIX you can check the manual page of date cmd.

date '+%Y%m%d'
$ DATE=20101018121445
$ DATE2=`echo $DATE | cut -c1-8`
$ echo $DATE2
20101018
1 Like

Something like this,

new_dt=$(echo "20101018121445" | cut -c1-8);echo $new_dt
DATE=`date +%Y%m%d%H%M`
NEW_DATE=`echo ${DATE:0:8}`

This will work in any POSIX shell:

date=20101018121445
yyyymmdd=${date%??????} ## remove the last six characters

In bash and ksh93 you can do:

yyyymmdd=${date:0:8}

Thank you so much for your time and reply.