Solved

How would you create an output date in the format dd/mm/yy using C# when the input is in the form yyyy/mm/dd?

Posted on 2011-09-23
4
549 Views
Last Modified: 2013-12-17
I am developing a C# consolde application using Visual Studio.

How would you modify the following code to create a date in the format dd/mm/yy to replace my current outupt of dd/mm/yyyy?

 if (reader.Name == "csc:processing_date")
                        {
                            arr1[i, 13] = "GROUP_FIELD_NAME:CheckPaidDate";
                            String d = reader.ReadElementContentAsString();
                            DateTime parsedDate;
                            IFormatProvider format = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat;

                            if (DateTime.TryParseExact(d, "yyyyMMdd", format, DateTimeStyles.None, out parsedDate))
                                arr1[i, 14] = "GROUP_FIELD_VALUE:" + parsedDate.ToString("MM/dd/yyyy");
                            else
                            {
                                arr1[i, 14] = "GROUP_FIELD_VALUE:" + parsedDate.ToString("MM/dd/yyyy");
                                sw1.WriteLine("GROUP_FIELD_NAME:ItemSequence:" + itemsequence + ",GROUP_FIELD_NAME:CheckPaidDate" + "," + "GROUP_FIELD_VALUE:" + d);
                                iCheckNumber--;
                            }
                        }                    
0
Comment
Question by:zimmer9
  • 2
4 Comments
 

Author Comment

by:zimmer9
ID: 36588957
I guess it is this simple?

if (DateTime.TryParseExact(d, "yyyyMMdd", format, DateTimeStyles.None, out parsedDate))
                                arr1[i, 14] = "GROUP_FIELD_VALUE:" + parsedDate.ToString("MM/dd/yy");
                            else
                            {
                                arr1[i, 14] = "GROUP_FIELD_VALUE:" + parsedDate.ToString("MM/dd/yy");
                                sw1.WriteLine("GROUP_FIELD_NAME:ItemSequence:" + itemsequence + ",GROUP_FIELD_NAME:CheckPaidDate" + "," + "GROUP_FIELD_VALUE:" + d);
                                iCheckNumber--;
                            }
                        }                    
0
 
LVL 12

Expert Comment

by:starlite551
ID: 36589060
Try this method :
public static DateTime ConvertDate(string date, string pattern)
{
    DateTime retval = DateTime.MinValue;
    if (System.DateTime.TryParseExact(date, pattern, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out retval))
       return retval;

    // Could not convert date..
    return DateTime.MinValue;
}


DateTime myDate = ConvertDate("20101230", "yyyyMMdd");

Open in new window

0
 
LVL 12

Assisted Solution

by:starlite551
starlite551 earned 250 total points
ID: 36589070
Or This One!!
CultureInfo provider = CultureInfo.CreateSpecificCulture("en-UK");   

string dateString = "19850121"; //in yyyyMMdd format 

string oldFormat = "yyyyMMdd"; 

DateTime result = DateTime.ParseExact(dateString, oldFormat, provider); //MM/dd/yyyyformat

Open in new window

0
 
LVL 13

Accepted Solution

by:
jonnidip earned 250 total points
ID: 36589087
Please take a look at this method:
public static String Convert_DateTime_String_Format(String string_date, String format_input, String format_output)
        {
            return DateTime.ParseExact(string_date, format_input, CultureInfo.InvariantCulture, DateTimeStyles.None).ToString(format_output);
        }

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question