locked
Date Format Help - yyyy-mm-dd to CCYYMMDD RRS feed

  • Question

  • Hello All , 

    I have a mapping issue where i need to change the date format from 2016-10-18(YYYY-MM-DD)  to 21161018(CCYYMMDD)

    Please help me out !



    Sukra

    • Edited by sukra1234 Wednesday, October 19, 2016 9:45 PM
    Wednesday, October 19, 2016 9:45 PM

Answers

  • Hi

    You can use a C# inline scripting functoid, and connect it with the source and destination fields.

    Code in the script can be as follows-

    public string formatDate(string param1)
    {
    	return param1.Replace("-", "");
    }

    The other option is to parse the input date and format it, note that for invalid input date below code sets 0 in the output field. So the inline C# code in the functoid would be-

    public  string FormatDate(string inputDate)
            {
                DateTime date;
                string formatString = "yyyy-MM-dd";
                if (!DateTime.TryParseExact(inputDate, formatString, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date))
                {
                    return "0";
                }
                
                string outDate = date.ToString("yyyyMMdd");
                return outDate;
            }


    Thanks Arindam



    Thursday, October 20, 2016 3:40 AM
    Moderator

All replies

  • What you're trying to do is actually very easy.

    You would use a combination of:

    DateTime.TryParseExact(): https://msdn.microsoft.com/en-us/library/ms131044(v=vs.110).aspx

    And

    Custom DateTime Format String: https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Wednesday, October 19, 2016 10:40 PM
    Moderator
  • Hi

    You can use a C# inline scripting functoid, and connect it with the source and destination fields.

    Code in the script can be as follows-

    public string formatDate(string param1)
    {
    	return param1.Replace("-", "");
    }

    The other option is to parse the input date and format it, note that for invalid input date below code sets 0 in the output field. So the inline C# code in the functoid would be-

    public  string FormatDate(string inputDate)
            {
                DateTime date;
                string formatString = "yyyy-MM-dd";
                if (!DateTime.TryParseExact(inputDate, formatString, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date))
                {
                    return "0";
                }
                
                string outDate = date.ToString("yyyyMMdd");
                return outDate;
            }


    Thanks Arindam



    Thursday, October 20, 2016 3:40 AM
    Moderator
  • John & Arndam , 

    Arndam 

    The format what i need is "CCYYMMDD" (Century , Year , Month , Date )  , I hope the code which you given does only removing the dash .. 

    John , 

    The link which you gave doesnt have the date format for "CC" (Century)  



    • Edited by sukra1234 Thursday, October 20, 2016 1:39 PM
    Thursday, October 20, 2016 1:37 PM
  • Hi Sukra1234,

    Try the below

    //You do not need to create the string date as it will be availabel as input to the scripting functoid.
    
    
    
    string Date = DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd");
    
    DateTime dt;
    DateTime.TryParseExact(Date, "yyyy-MM-dd", System.Globalization.CultureInfo.InstalledUICulture, System.Globalization.DateTimeStyles.None, out dt);
    string date1 = dt.ToString("yyyyMMdd");
                     Console.WriteLine(date1);
                 
    return date1;
    Regards,

    Mandar Dharmadhikari

    Thursday, October 20, 2016 1:54 PM
    Moderator
  • Hi

    I had shared 2 snippets in my first post, one that removes the dashes, and a second one that parses and generates the date in the format you need. Both should work, prefer to use the 2nd one. Pasting the snippets again for your reference-

    public string formatDate(string param1)
    {
    	return param1.Replace("-", "");
    }

    The other option is to parse the input date and format it, note that for invalid input date below code sets 0 in the output field. So the inline C# code in the functoid would be-

    public  string FormatDate(string inputDate)
            {
                DateTime date;
                string formatString = "yyyy-MM-dd";
                if (!DateTime.TryParseExact(inputDate, formatString, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date))
                {
                    return "0";
                }
                
                string outDate = date.ToString("yyyyMMdd");
                return outDate;
            }


    Thanks Arindam



    Thursday, October 20, 2016 2:27 PM
    Moderator
  • By the way, CCYYMMDD and YYYYMMDD are the same thing. So for the year 2016, both CCYY and YYYY will be 2016.

    Thanks Arindam


    Thursday, October 20, 2016 2:30 PM
    Moderator
  • The link which you gave doesnt have the date format for "CC" (Century)  

    That's because Century is not an explicit date component.

    Century is treated as an implicit part of the Year so the two most common Year formats would be YY and YYYY.

    Your format string would be yyyyMMdd.

    Did you try?  The code will work, trust us.

    Thursday, October 20, 2016 2:42 PM
    Moderator