[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to format a date field in the format MM/DD/YY for a C# console application?

Posted on 2010-08-26
15
Medium Priority
?
618 Views
Last Modified: 2013-12-17
I am writing my first C# console application using Visual Studio 2005. Do you know how I could rewrite the following C# code "IF statement" so that the CheckPaidDate output field is formatted as MM/DD/YY?

An example of one of my daily input file records for the date field is as follows:
<csc:processing_date>20100817</csc:processing_date>

The output should be in format MM/DD/YY, for example:

Output fields: GROUP_FIELD_NAME:CheckPaidDate
                      GROUP_FIELD_VALUE:08/17/10

To handle this date field, I wrote the following C# IF statement:
 if (reader.Name == "csc:processing_date")
                    {
                        arr1[i,13] = "GROUP_FIELD_NAME:CheckPaidDate";
                        arr1[i,14] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();
                    }
-------------------------------
<?xml version="1.0"?>
<csc:CIndex_File
    xmlns:csc="http://c.com/xml/CIndex_File">
<csc:header>
    <csc:version>1.0</csc:version>
    <csc:customer_name>              </csc:customer_name>
    <csc:request_id>MD </csc:request_id>
    <csc:creation_date>20100805</csc:creation_date>
    <csc:creation_time>084027</csc:creation_time>
    <csc:creation_host>hbd-chvcore</csc:creation_host>
    <csc:content_type>CHECK</csc:content_type>
    <csc:item_count>2</csc:item_count>
    <csc:image_file_name>J.img</csc:image_file_name>
    <csc:input_request_file></csc:input_request_file>
</csc:header>
<csc:item>
    <csc:processing_date>20100805</csc:processing_date>
    <csc:item_sequence_number>000000000000001</csc:item_sequence_number>
    <csc:account_number>00000000000000000005 </csc:account_number>
    <csc:check_number>000000000000001</csc:check_number>
    <csc:amount>0000000100</csc:amount>
    <csc:routing_transit>000000007</csc:routing_transit>
    <csc:bank_number>0002</csc:bank_number>
    <csc:transaction_code></csc:transaction_code>
    <csc:data1></csc:data1>
    <csc:data2></csc:data2>
    <csc:data3></csc:data3>
    <csc:userField></csc:userField>
    <csc:image_offset>0000000000</csc:image_offset>
    <csc:image_length>0000016685</csc:image_length>
    <csc:image_side>A</csc:image_side>
</csc:item>
<csc:item>
    <csc:processing_date>20100805</csc:processing_date>
    <csc:item_sequence_number>000000000000002</csc:item_sequence_number>
    <csc:account_number>00000000000000000005</csc:account_number>
    <csc:check_number>000000000000002</csc:check_number>
    <csc:amount>0000000200</csc:amount>
    <csc:routing_transit>000000007</csc:routing_transit>
    <csc:bank_number>0002</csc:bank_number>
    <csc:transaction_code></csc:transaction_code>
    <csc:data1></csc:data1>
    <csc:data2></csc:data2>
    <csc:data3></csc:data3>
    <csc:userField></csc:userField>
    <csc:image_offset>0000000000</csc:image_offset>
    <csc:image_length>0000016685</csc:image_length>
    <csc:image_side>A</csc:image_side>
</csc:item>
</csc:CheckVision_Index_File>

My program is as follows:

using System;
using System.Xml;
using System.IO;
namespace ReadXml1
{
     class Class1
     {        
         static void Main(string[] args)
         {
            int i = 0;
            string[,] arr1 = new string[1000,19];
            int iCheckNumber = 0;
           
            XmlTextReader reader = new XmlTextReader("C:\\rbc.xml");
     
            StreamWriter sw = new StreamWriter("C:\\output.txt");

            while (reader.Read())
             
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "csc:check_number")
                    {
                        iCheckNumber = iCheckNumber + 1;
                        //sw.WriteLine("COMMENT: CHECK NUMBER # {0}", iCheckNumber);
                        //sw.WriteLine("GROUP_FIELD_NAME:CheckNumber");
                        //sw.WriteLine("GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString());
                        arr1[i,0] = "COMMENT: CHECK NUMBER #" + iCheckNumber;
                        arr1[i,1] = "GROUP_FIELD_NAME:CheckNumber";
                        arr1[i,2] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();        
                    }
                    if (reader.Name == "csc:routing_transit")
                    {
                        arr1[i,3] = "GROUP_FIELD_NAME:RoutingTransit";
                        arr1[i,4] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();
                        arr1[i,5] = "GROUP_FIELD_NAME:BankName";
                        arr1[i,6] = "GROUP_FIELD_VALUE:RBC BANK";

                        //sw.WriteLine("GROUP_FIELD_NAME:RoutingTransit");
                        //sw.WriteLine("GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString());
                        //sw.WriteLine("GROUP_FIELD_NAME:BankName");
                        //sw.WriteLine("GROUP_FIELD_VALUE:RBC BANK");
                    }
                    if (reader.Name == "csc:account_number")
                    {
                        //sw.WriteLine("GROUP_FIELD_NAME:BankAccountNo");
                        //sw.WriteLine("GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString());                        
                        arr1[i,7] = "GROUP_FIELD_NAME:BankAccountNo";
                        arr1[i,8] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();
                    }
                    if (reader.Name == "csc:amount")
                    {
                        //sw.WriteLine("GROUP_FIELD_NAME:CheckAmount");
                        //sw.WriteLine("GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString());
                        //sw.WriteLine("GROUP_FIELD_NAME:CpscNo");
                        //sw.WriteLine("GROUP_FIELD_VALUE:00000000");
                        arr1[i,9] = "GROUP_FIELD_NAME:CheckAmount";
                        arr1[i,10] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();
                        arr1[i,11] = "GROUP_FIELD_NAME:CpscNo";
                        arr1[i,12] = "GROUP_FIELD_VALUE:00000000";                        
                    }
                    if (reader.Name == "csc:processing_date")
                    {
                        //sw.WriteLine("GROUP_FIELD_NAME:CheckPaidDate");
                        //sw.WriteLine("GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString());
                        //sw.WriteLine("GROUP_FIELD_NAME:OfficeNo");
                        //sw.WriteLine("GROUP_FIELD_VALUE:000");
                        arr1[i,13] = "GROUP_FIELD_NAME:CheckPaidDate";
                        arr1[i,14] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();
                        arr1[i,15] = "GROUP_FIELD_NAME:OfficeNo";
                        arr1[i,16] = "GROUP_FIELD_VALUE:000";  
                    }
                    if (reader.Name == "csc:image_offset")
                    {
                        //sw.WriteLine("GROUP_OFFSET:" + reader.ReadElementContentAsString());
                        arr1[i,17] = "GROUP_OFFSET:" + reader.ReadElementContentAsString();
                    }
                    if (reader.Name == "csc:image_length")
                    {
                        //sw.WriteLine("GROUP_LENGTH:" + reader.ReadElementContentAsString());
                        arr1[i,18] = "GROUP_LENGTH:" + reader.ReadElementContentAsString();
                        i++;
                    }                    
                }            
            }

            for (int iter = 0; iter < i; iter++)
            {
                for (int j = 0; j < 19; j++)
                {
                    sw.WriteLine(arr1[iter,j]);
                }
            }
            sw.Close();
            reader.Close();    
         }                  
     }
}

My output file would look like the following:

COMMENT: CHECK NUMBER #1
GROUP_FIELD_NAME:CheckNumber
GROUP_FIELD_VALUE:000000000000001
GROUP_FIELD_NAME:RoutingTransit
GROUP_FIELD_VALUE:000000007
GROUP_FIELD_NAME:BankName
GROUP_FIELD_VALUE:RBC BANK
GROUP_FIELD_NAME:BankAccountNo
GROUP_FIELD_VALUE:00000000000000000005
GROUP_FIELD_NAME:CheckAmount
GROUP_FIELD_VALUE:0000001.00
GROUP_FIELD_NAME:CpscNo
GROUP_FIELD_VALUE:00000000
GROUP_FIELD_NAME:CheckPaidDate
GROUP_FIELD_VALUE:20100805
GROUP_FIELD_NAME:OfficeNo
GROUP_FIELD_VALUE:000
GROUP_OFFSET:0000000000
GROUP_LENGTH:0000016685
COMMENT: CHECK NUMBER #2
GROUP_FIELD_NAME:CheckNumber
GROUP_FIELD_VALUE:000000000000002
GROUP_FIELD_NAME:RoutingTransit
GROUP_FIELD_VALUE:000000007
GROUP_FIELD_NAME:BankName
GROUP_FIELD_VALUE:RBC BANK
GROUP_FIELD_NAME:BankAccountNo
GROUP_FIELD_VALUE:00000000000000000005
GROUP_FIELD_NAME:CheckAmount
GROUP_FIELD_VALUE:0000002.00
GROUP_FIELD_NAME:CpscNo
GROUP_FIELD_VALUE:00000000
GROUP_FIELD_NAME:CheckPaidDate
GROUP_FIELD_VALUE:20100805
GROUP_FIELD_NAME:OfficeNo
GROUP_FIELD_VALUE:000
GROUP_OFFSET:0000000000
GROUP_LENGTH:0000016685
0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 3
  • +1
15 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33536199
So basically you want to convert a string to a date? Could you use Convert.ToDateTime?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33536212
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33536271
oh, forgot to add regarding the date 20100805. You can create a new string with delimiters using substring or you can create a new DateTime variable instead of converting passing in the values
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 65

Expert Comment

by:rockiroads
ID: 33536312
something like this I think

DateTime d = new DateTime(x.substring(0,4),x.substring(4,2),x.substring(6,2))

then output and format d

Cant remember if its optional to specify time as well, if so then any dummy time values will suffice
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33536318
IFormatProvider provider = new System.Globalization.CultureInfo("en-US", true);
            String datetime = "2010/08/26";
            DateTime dt = DateTime.Parse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);

it returns the date as MM/dd/yyyy
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33536336
Just change the culture to which ever format you want. If you want to british you can just change the culture info to "en-GB" and you will get date in the format

dd/mm/yy
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33536419
thinking about it, if you just wanted to display the date as a string without any further use how about creating a new string yourself?
eg

String newdd = d.Substring(6, 2) + "/" + d.Substring(4, 2) + "/" + d.Substring(0, 4);

where d is your 20100805 date
0
 

Author Comment

by:zimmer9
ID: 33536448
I read the following input record:

<csc:processing_date>20100817</csc:processing_date>

and then I would modify the following IF statement with a method?

if (reader.Name == "csc:processing_date")
                    {
                        arr1[i,13] = "GROUP_FIELD_NAME:CheckPaidDate";
                        arr1[i,14] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();
                    }
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33536519
Are you try to compare the format of the date or exact date value. If you try to compare the format then you can use the if else statement to convert the date and then assign that value to checkpaiddate.
0
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 800 total points
ID: 33536592
do you want to do anything else with the date or is it strictly for dumping on screen only?

So if you had this maybe

String d = reader.ReadElementContentAsString();
String formattedDate = d.Substring(6, 2) + "/" + d.Substring(4, 2) + "/" + d.Substring(0, 4);
arr1[i,14] = "GROUP_FIELD_VALUE:" + formattedDate;

No validation obviously otherwise SubString calls will fail. It assumes date exists and is always in YYYYMMDD format
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33536692
It should be:

String d = reader.ReadElementContentAsString();
String formattedDate = d.Substring(6, 2) + "/" + d.Substring(4, 2) + "/" + d.Substring(2, 2);
arr1[i,14] = "GROUP_FIELD_VALUE:" + formattedDate;
0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 1200 total points
ID: 33536713
Ooops, here it is in MM/DD/YY format:

  String d = reader.ReadElementContentAsString();
String formattedDate = d.Substring(4, 2) + "/" + d.Substring(6, 2) + "/" + d.Substring(2, 2);
arr1[i,14] = "GROUP_FIELD_VALUE:" + formattedDate;
                               
 
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33536719
Oops, thanks Zhaolai, put the date format as DD/MM/YYYY. Hopefully zimmer got the jist of the idea with regards to using substring though
0
 

Author Comment

by:zimmer9
ID: 33536778
do you want to do anything else with the date or is it strictly for dumping on screen only?
--------------
I just want to create an extract file from an input file.
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33536814
Then you just need a string, not a date. The code in my last post will work for you.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 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