Hi all

I want to create a method that converts from one time or date format (as a string) to another by specifying the inputformat and the outputformat. I can do the output as below but I'm struggling on how to specify what the input format is. Can anyone help?
thanks.

//I'm halfway there.
string date = FormatDateTime(myDate, "yyyyMMdd"));
string time = FormatDateTime(myTime, "HHmm"));


        private string FormatDateTime(string dateortime, string outputformat)
        {
            string ret = "";
            if (dateortime == "")
                return "";
            try
            {
                ret = System.Convert.ToDateTime(dateortime).ToString(outputformat);
            }
            catch
            {
            }
            return ret;
        }

Use DateTime.TryParseExact

SUMMARY:
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.

Comments
gave more than a link :-)

DateTime.TryParseExact is exactly what I need. Works perfectly :-)

This question has already been answered. Start a new discussion instead.