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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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