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
339 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
[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
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 36545149
parsedDate.ToString("mm/dd/yyyy");
0
 

Author Comment

by:zimmer9
ID: 36545362
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
ID: 36545394
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
ID: 36545449
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
ID: 36545486
sorry i used lowercase m and it should be upper case
.ToString("MM/dd/yyyy');
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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