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
541 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

770 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