Date Reformatting function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date Reformatting function
# 1  
Old 08-24-2014
Date Reformatting function

Hi ,

I am trying to create a function with below requirements

1. It will take two parameters as Input. Date and Date format
2. Output will be in YYYYMMDD format.

Example 1:
Code:
Input: fn_date_reformatter('01-AUG-2014','DD-MON-YYYY')
Output: 20140801

Example 2:
Code:
Input: fn_date_reformatter('01-08-2014','DD-MM-YYYY')
Output: 20140801

Example 3:
Code:
Input: fn_date_reformatter('01082014','DDMMYYYY')
Output: 20140801


Example 4:
Code:
Input: fn_date_reformatter('20140801','YYYYMMDD')
Output: 20140801

Example 5:
Code:
Input: fn_date_reformatter('2014-08-01','YYYY-MM-DD')
Output: 20140801

Can someone please help me or guide me to the right direction?
# 2  
Old 08-24-2014
This is posted in the Shell Programming and Scripting forum, but the way you are invoking your functions looks like you want to write a C function (except for using single quotes instead of double quotes surrounding strings).

If you are trying to do this in a shell function, it might not be too hard if you have GNU date with the -d "date" option or a recent ksh93 with printf '%(format)T" "date" although the documentation about the recognized formats for the date operand is sparse for both.

If you're trying to do it in C, look at the strptime and strftime function man pages.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 08-24-2014
I have GNU `date` in installed in my Unix box. Can you show some example. I got some from the unix forum but haven't got all types of input formats.
# 4  
Old 08-24-2014
I don't have GNU date on my system. The Linux date man page available on this forum says:
Quote:
DATE STRING
The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb
2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date string may
contain items indicating calendar date, time of day, time zone, day of week, relative
time, relative date, and numbers. An empty string indicates the beginning of the day.
The date string format is more complex than is easily documented here but is fully
described in the info documentation.
Since you have access to the GNU date utility, I assume you can find "the info documentation" that fully describes the date string format on your system. It isn't available on my system.

The main problem you'll need to resolve is how to tell the difference between an MMDD versus DDMM input or how to rearrange your input into the order that date will interpret correctly for the input format you're given.
# 5  
Old 08-25-2014
Try this (assuming you want it done in shell script), which is just juggling chars, not doing any validity checking etc., nor month name conversions:
Code:
fn_date_reformatter() {
                IDXD=${2%%D*}
                IDXM=${2%%M*}
                IDXY=${2%%Y*}
                echo ${1:${#IDXY}:4}${1:${#IDXM}:2}${1:${#IDXD}:2}    
                }
fn_date_reformatter '20140801' 'YYYYMMDD'
20140801
fn_date_reformatter '01-08-2014' 'DD-MM-YYYY'
20140801
fn_date_reformatter '01082014' 'DDMMYYYY'
20140801
fn_date_reformatter '2014-08-01' 'YYYY-MM-DD'
20140801

---------- Post updated at 12:47 ---------- Previous update was at 12:00 ----------

Try this with month name conversion:
Code:
fn_date_reformatter() {
        M=JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC
        IXO="${2%%MON*}"
        IXD="${2%%DD*}"
        IXM="${2%%MM*}"
        IXY="${2%%YY*}"
        printf "%s" ${1:${#IXY}:4}
        [ "${IXO}" != "$2" ] &&
             { L="${M%${1:${#IXO}:3}*}"
              printf "%02d" $((${#L}/3+1))
            } ||
              printf "%s" ${1:${#IXM}:2}
        printf "%s\n" ${1:${#IXD}:2}
        }
fn_date_reformatter '01-AUG-2014' 'DD-MON-YYYY'
20140801
fn_date_reformatter 'AUG-01-2014' 'MON-DD-YYYY'
20140801

(Looks a bit clumsy with all those abbreviated variable names...)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Date reformatting

I have been reformatting dates from a data file to make them mysql compliant. 31-10-2011 Loc1 1-11-2011 Loc2 The first can be captured by this: sed -i '' -e "s#\(..\)-\(..\)-\(....\)#\3-\2-\1#" data.txt and leads to: 2011-10-31 Loc1 The second line is captured as follows: sed -i... (2 Replies)
Discussion started by: figaro
2 Replies

2. UNIX for Dummies Questions & Answers

Date reformatting

I have a file with temperature measurements: Loc1,20090102,71.55 Loc1,20090103,71.65 Loc1,20090104,71.55 Loc1,20090105,71.54 Loc1,20090106,71.54 However, to load this into a database I would like to reformat the dates (column 2) from the yyyymmdd format to the yyyy-mm-dd format. I have... (2 Replies)
Discussion started by: figaro
2 Replies

3. UNIX for Dummies Questions & Answers

Date reformatting

I currently have the following file containing sample values for a number of dates: Loc1 04 Jan 2007 0.95 0.9532 Loc1 05 Jan 2007 0.95 0.9513 Loc1 06 Jan 2007 0.95 0.9535 This continues for all months of the year and spans across several years. I am trying to reformat the dates so that... (2 Replies)
Discussion started by: figaro
2 Replies

4. Shell Programming and Scripting

Date function

I read man page for etc/shadow field.. on the 8th field, i assume that's the field to change account expire date. my question is: What value does the 8th field keep? i assume it's 13514 instead of "Date" value such as 11/10/08. on the man page, it said: " expire value = 13514 = jan 1,... (11 Replies)
Discussion started by: c00kie88
11 Replies

5. Shell Programming and Scripting

Date Function

Hi, My file format is: E102,0,21-04-2007,0,2/25/1994,E003,A,125400,10450,60.2884620 E103,0,21/04/2007,0,2/2/1996,E003,A,125400,10450,60.2884620 E104,0,04/21/2007,0,2/2/1996,E003,A,125400,10450,60.2884620 E105,0,21-APR-2007,0,2/2/1996,E003,A,125400,10450,60.2884620... (1 Reply)
Discussion started by: charandevu
1 Replies

6. Shell Programming and Scripting

Date Function

Hi, My file format is: E102,0,21-04-2007,0,2/25/1994,E003,A,125400,10450,60.2884620 E103,0,21/04/2007,0,2/2/1996,E003,A,125400,10450,60.2884620 E104,0,04/21/2007,0,2/2/1996,E003,A,125400,10450,60.2884620 E105,0,21-APR-2007,0,2/2/1996,E003,A,125400,10450,60.2884620... (1 Reply)
Discussion started by: charandevu
1 Replies

7. Shell Programming and Scripting

Wrong date function

Hi, I am getting some very strange output when using date function in PERL on Solaris. Infact the month portion is wrong and it is 1 less then the current, means today it is responding as month =3 , andthis should be 4 ------> April Any help my code is ($day, $month, $year) =... (3 Replies)
Discussion started by: Asteroid
3 Replies

8. Shell Programming and Scripting

date function

hi, I have to ftp previous days file from a directory to another location. The name of the files are like "xxx20060225" (yyyymmdd format) "xxx20060226" ls -lrt xxx*| tail -2| head -1 will give me the file, but if i could get anything... (2 Replies)
Discussion started by: abey
2 Replies

9. UNIX for Dummies Questions & Answers

Yesterday's date function

I am using this function to calculate yesterday's date and return it in the following format: Jan 09 date '+%b %d %Y' | { read MONTH DAY YEAR DAY=`expr "$DAY" - 1` case "$DAY" in 0) MONTH=`expr "$MONTH" - 1` case "$MONTH" in ... (4 Replies)
Discussion started by: ssmiths001
4 Replies

10. UNIX for Dummies Questions & Answers

Date function question

hi guys! just want to ask if you could help me with the sript i'm working on. i need to automatically generate a summarized report everyday for all transactions the day before and ftp it to another machine. my only problem is that i need to name the file as the date yesterday. for example if i... (12 Replies)
Discussion started by: crpalermo
12 Replies
Login or Register to Ask a Question