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

#4
July 1, 2020 at 03:40:15
With bash and GNU date, you can do :

$ yyddd=20033
$ date --date "${yyddd:0:2}-01-01 + ${yyddd:2:3} days - 1 day" +'%Y%m%d'
20200202
$

$ yyddd=13016
$ date --date "${yyddd:0:2}-01-01 + ${yyddd:2:3} days - 1 day" +'%Y%m%d'
20130116
$

message edited by lcjp


Report •

Ask Question