Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
398 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 2000 total points
ID: 36545486
sorry i used lowercase m and it should be upper case
.ToString("MM/dd/yyyy');
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 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