change yyddd to yyyymmdd in unix

April 25, 2013 at 03:42:09
Specs: Windows XP
how to convert date format yyddd to yyyymmdd format in unix shell scripting....................................

See More: change yyddd to yyyymmdd in unix

Report •

#1
April 25, 2013 at 07:40:16
#!/bin/ksh

typeset -Z2 dd # 2 digit day
typeset -Z2 mm # 2 digit month

# example date
yrdd="13016"

yr=20$(echo $yrdd|cut -c1-2)
dd=$(echo $yrdd|cut -c3-5)

# assume the month comes from today's date
mm=$(date '+%m')
yrmmdd=$yr$mm$dd
echo $yrmmdd


Report •

#2
April 25, 2013 at 08:12:47
Different version. Not every *nix version supports 'typeset -L':

#!/bin/ksh

typeset -Z2 dd # 2 digit day
typeset -Z2 mm # 2 digit month
typeset -L2 yy # 2 digit year

# example date
yrdd="13016"
yy=$yrdd
dd=$yrdd

# assume the month comes from today's date
mm=$(date '+%m')
yrmmdd=20$yy$mm$dd
echo $yrmmdd


Report •

#3
August 29, 2017 at 06:50:36
I see no reasons.

Report •
Related Solutions


Ask Question