Re: Custom Date Functiod

Home Page Forums BizTalk 2004 – BizTalk 2010 Custom Date Functiod Re: Custom Date Functiod

#24094

The code below is for a custom functoid that I wrote to accomplish what you are asking.  It takes two parameters in, date and format.  It will use the DateTime.Parse() method to convert the date parameter to a DateTime type.  It will then convert the date back to a string based on the .NET DateTime.ToString() formatting pattern specified in the format parameter.  All of the standard .NET formatting patterns will work plus I added an extra to output Julian day by using “jjj” in the pattern.

Please let me know what you think!

 

[code]

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.BizTalk.BaseFunctoids;
using System.Reflection;
using System.Globalization;
using System.Resources;

namespace BizTalk.CustomFunctoid
{
    public class DateFormat : BaseFunctoid
    {
        static ResourceManager resmgr = new ResourceManager(“Meritain.BizTalk.CustomFunctoid.CustomResource”, Assembly.GetExecutingAssembly());

        public DateFormat()
            : base()
        {
                    // This has to be a number greater than 6000
            this.ID = System.Convert.ToInt32(resmgr.GetString(“IDS_DATEFORMAT_ID”));
           
            this.SetupResourceAssembly(“Meritain.BizTalk.CustomFunctoid.CustomResource”, Assembly.GetExecutingAssembly());

                    //Set Resource strings , bitmaps
            this.SetName(“IDS_DATEFORMAT_NAME”);
            this.SetTooltip(“IDS_DATEFORMAT_TOOLTIP”);
            this.SetDescription(“IDS_DATEFORMAT_DESCRIPTION”);
            this.SetBitmap(“IDS_DATEFORMAT_ICON”);

                    // Minimum and maximum parameters that the  functoid accepts
            this.SetMinParams(2);
            this.SetMaxParams(2);

                    // Function name that needs to be called when this Functoid is invoked.
                    // Put this in GAC
            this.SetExternalFunctionName(GetType().Assembly.FullName, “Meritain.BizTalk.CustomFunctoid.DateFormat”, “FormatDate”);

                    //Category for this functoid
            this.Category = FunctoidCategory.DateTime;

                    //Input and output Connection type
            this.OutputConnectionType = ConnectionType.AllExceptRecord;
            this.AddInputConnectionType(ConnectionType.AllExceptRecord);
        }

        public String FormatDate(String dateIn, String formatOut)
        {
            String dateToFormat = dateIn.Trim();
            DateTime theDate;
            String dateFormatted = String.Empty;

            try
            {
                        // Try to parse the date string passed through the map.    /* .DayOfYear.ToString(“000”) */
                theDate = DateTime.Parse(dateToFormat);

                dateFormatted = this.ApplyFormatting(theDate, formatOut);
            }
            catch
            {
                try
                {
                            // “yyyyMMdd” cannot be parsed so try to format the date string to “MM/dd/yyyy” and reparse if parsing fails.
                    CultureInfo nfo = new CultureInfo(“en-US”);
                    theDate = DateTime.ParseExact(dateToFormat, “yyyyMMdd”, nfo);

                    dateFormatted = this.ApplyFormatting(theDate, formatOut);
                }
                catch
                {
                    dateFormatted = String.Empty;
                }
            }           
           
            return dateFormatted;
        }

        private String ApplyFormatting(DateTime d, String f)
        {
            String s = String.Empty;

                    // Check for lonely Julian Day and assign it to return value
            if (f.Equals(“jjj”))
            {
                s = d.DayOfYear.ToString(“000”);
            }
            else
            {
                        // Insert Julian Day where specified in date format string
                s = d.ToString(f);
                if (f.Contains(“jjj”))
                    s = s.Replace(“jjj”, d.DayOfYear.ToString(“000”));
            }

            return s;
        }
    }
}

[/code]

 

-Dave