Solved

How to create a date with a leading zero in the format mm/dd/yyyyy for January thru September for a C# program?

Posted on 2011-09-15
5
287 Views
Last Modified: 2013-12-17
I am writing a C# console application using Visual Studio 2005.

In the following code, is there a way to ensure that the month will be filled with a leading zero for the calendar months January through September, when writing out their numeric equivalent?

parsedDate.ToShortDateString(); for example, 9/13/2011 should be written as 09/13/2011
----------------------------------------

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.ToShortDateString();
   else
   {
      arr1[i, 14] = "GROUP_FIELD_VALUE:" + parsedDate.ToShortDateString();
      sw1.WriteLine("GROUP_FIELD_NAME:ItemSequence:" + itemsequence + ",GROUP_FIELD_NAME:CheckPaidDate" + "," + "GROUP_FIELD_VALUE:" + d);
      iCheckNumber--;
   }
}
0
Comment
Question by:zimmer9
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
parsedDate.ToString("mm/dd/yyyy");
0
 

Author Comment

by:zimmer9
Comment Utility
How do I rewrite the below expression using parsedDate.ToString("mm/dd/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.ToShortDateString();
   else
   {
      arr1[i, 14] = "GROUP_FIELD_VALUE:" + parsedDate.ToShortDateString();
      sw1.WriteLine("GROUP_FIELD_NAME:ItemSequence:" + itemsequence + ",GROUP_FIELD_NAME:CheckPaidDate" + "," + "GROUP_FIELD_VALUE:" + d);
      iCheckNumber--;
   }
}
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
although im not sure what your culture is -- the best i can do is this....
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--;
   }
}

Open in new window

0
 

Author Comment

by:zimmer9
Comment Utility
The input is as follows with 09 as the month:

<csc:processing_date>20110913</csc:processing_date>

The output is 00 for the month of September which is wrong, it should be 09:

GROUP_FIELD_NAME:CheckPaidDate
GROUP_FIELD_VALUE:00/13/2011
0
 
LVL 22

Accepted Solution

by:
p_davis earned 500 total points
Comment Utility
sorry i used lowercase m and it should be upper case
.ToString("MM/dd/yyyy');
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now