Search code examples
crystal-reports

Advanced conversion of a single number to year, along with hyphen


I want to convert the string value to datetime in crystal report

date format are

05-10-7-AAAA (mm-dd-year(2017)-AAAA)
05-23-3-00   (mm-dd-year(2013)-00)
...

My desired output is

10-May-2017
23-May-2013
...

My good reference has been How to convert string value to proper datetime format & the following attempts threw me errors:

Date({ORDERCHECKVIEW.LOTNUMBER}[4 to 5], ***{ORDERCHECKVIEW.LOTNUMBER}[1 to 2], {ORDERCHECKVIEW.LOTNUMBER}[7]***)  //Too many arguments have been given to this function

&

DateValue({ORDERCHECKVIEW.LOTNUMBER})  //Bad date format string

Thank you so much in advance and I will be as responsive as I can!


Solution

  • Create a new Formula and apply the code below. {Command.mydate} will be replaced with your field

    local StringVar mDate;
    local StringVar mMonth;
    local StringVar mDay;
    local StringVar mYear;
    //local StringVar mSuffix:= StrReverse(left(StrReverse({Command.mydate}),instr(StrReverse({Command.mydate}),"-",1)-1));    // to get suffix
    
    mDate:= left({Command.mydate},InStrRev ({Command.mydate},"-" )-1);     // get dd
    mMonth:= left(mDate,2);  // get MM
    mDay := Mid (mDate,4 ,2 );  // get YYYY
    mYear := totext(2010 + CDbl (right(mDate,InStr(StrReverse(left({Command.mydate},InStrRev ({Command.mydate},"-" )-1)),"-") -1 )),0,""); // transform   2017
    
    cDate(cDbl(mYear),cDbl(mMonth),cDbl(mDay));   // this will display dd-MM-YYYY     may not display as 01 for dd but you can always customise this in the GUI the important point is this is now a date field