Operating System - HP-UX
1825882 Members
3004 Online
109689 Solutions
New Discussion

convert number string to date format

 
SOLVED
Go to solution
Selvamuthu Kumaran
New Member

convert number string to date format

I need to combine a date string of format YYMMDD and time string of format HHMMSS into a date description with day, month and time

e.g.

The input strings are
DATESTR=040927
TIMESTR=151421

I need to convert it to:

Mon Sep 27 15:14:21 SST 2004

Could anyone help on this. Thanks.
6 REPLIES 6
Muthukumar_5
Honored Contributor
Solution

Re: convert number string to date format

We can do this with script as,

#!/usr/bin/ksh
# time.ksh

### input ####
DATE=$1
TIME=$2

####

day=$(echo $DATE | cut -c 1,2)
mon=$(echo $DATE | cut -c 3,4)
date=$(echo $DATE | cut -c 5,6)

####

hour=$(echo $TIME | cut -c 1,2)
min=$(echo $TIME | cut -c 3,4)
sec=$(echo $TIME | cut -c 5,6)

####

DAY="Sun Mon Tue Wed Thur Fri Sat"
MON="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"

# Array #
set -A DARR $DAY
set -A MARR $MON

year=$(date +"%Y")
zone=$(date +"%Z")

DD=${DARR[$day]}
MM=${MARR[$((mon-1))]}
echo "$DD $MM $date $hour:$min:$sec $zone $year"

# End #
exit 0

Save as,

time.ksh
$ksh time.ksh 040927 151421

You will get your desired output
Easy to suggest when don't know about the problem!
Selvamuthu Kumaran
New Member

Re: convert number string to date format

Hi MuthuKumar,

Thanks. It is a good one. But there is a little bug.

I got the output like this after I run the script.

Thu Sep 27 15:14:21 SST 2004

But it is suppose to be

Mon Sep 27 15:14:21 SST 2004

i.e the weekday goes wrong.
Muthukumar_5
Honored Contributor

Re: convert number string to date format

As standard,

date is followed as,

DAY="Sun Mon Tue Wed Thur Fri Sat"
00 01 02 03 04 05 06

IF you want to make that to Mon=04 then change it accordingly there in the DAY array.
Easy to suggest when don't know about the problem!
Michael D'Aulerio
Regular Advisor

Re: convert number string to date format

Hi Selvamuthu/Muthukumar,

Muthukumar, your script looks good but I hope you don't mind if I take it and correct some errors. Getting the day of the week is tricky. This is the best I have so far and its very convoluted. I'm sure there's a perl guru out there who could do this in 1 or 2 lines.

# The first 2 chars of DATESTR are the year, not the day.
# To get the day of week, you have to use a starting date.
# and proceed forward year by year, day by day to the date specified.
# Let's use Jan 1, 1900 which was a Monday

year=$(echo $DATESTR | cut -c 1,2)
mon=$(echo $DATESTR | cut -c 3,4)
dayofmonth=$(echo $DATESTR | cut -c 5,6)

if [[ $year -lt 5 ]]
then
# Assume that 00 thru 04 is 21st century, not 20th
year=20$year
else
year=19$year
fi

numyears=$(($year - 1900))
numleapdays=$((($year - 1900) / 4))

# Now adjust number of leap days for dates before March 1 of a leap year
if [[ $(($numyears % 4)) -eq 0 ]]
then
if [[ $mon -lt 3 ]]
then
numleapdays=$(($numleapdays - 1))
fi
fi
# Number of days to Jan 1 of the year
numdays=$((($numyears * 365) + $numleapdays))

set -A dayspermonth 0 31 59 90 120 151 181 212 243 273 304 334

numdays=$(($numdays + ${dayspermonth[$((mon-1))]} + $dayofmonth - 1))

DAY="Sun Mon Tue Wed Thur Fri Sat"

# Array #
set -A DARR $DAY

DD=${DARR[$((numdays % 7))]}

Mike D'.
Email: michael.n.daulerio@lmco.com
Muthukumar_5
Honored Contributor

Re: convert number string to date format

Micheal,

It is good to have review on script and

1> We are asked to format as like "Mon Sep 27 15:14:21 SST 2004" from input's there DATESTR=040927
TIMESTR=151421

so that we have to calculate with normal command date +"%Y" for year 2004 and for time zone date +"%Z"

DATESTR=04 09 27
Day management is as like Sun Mon... etc.
Month as like in the month array Jan..
day is as like given in the ouptut

TIMESTR=151421

Just put : as 15:14:21

And required script has not be used to set more time here :-) It is just to display formation of "date" command there.

2> It is good to see your analysis on time and date. It is helpful.

-Best Regards-
Easy to suggest when don't know about the problem!
Selvamuthu Kumaran
New Member

Re: convert number string to date format

Hi Mike/Muthukumar,

Thanks for your help.