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:

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

Example 2:

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

Example 3:

Input: fn_date_reformatter('01082014','DDMMYYYY')
Output: 20140801

Example 4:

Input: fn_date_reformatter('20140801','YYYYMMDD')
Output: 20140801

Example 5:

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

Can someone please help me or guide me to the right direction?

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.

1 Like

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.

I don't have GNU date on my system. The Linux date man page available on this forum says:

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.

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:

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:

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...)