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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

837 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