Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 437877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:37:20+00:00 2026-05-12T20:37:20+00:00

I was greeted with a nasty bug today. The task is pretty trivial, all

  • 0

I was greeted with a nasty bug today. The task is pretty trivial, all I needed to do is to convert the DateTime object to string in “yyyymmdd” format. The “yyyymmdd” part was stated in the development doc from the external software vendor. So, I conveniently copied the string from their file and pasted to my code. So I got the next

public string GetDateString(DateTime dateTime)
{
    return dateTime.ToString("yyyymmdd");
}

Pretty simple. So simple that I didn’t feel like to unit test the method. 20 minutes later, when other parts of my component are done. I started the app to check if things went right. Almost immediately I notice some supposed-to-be date field in my web page is displaying 20091511! This can’t be right, there is no 15th month of a year. So, I rushed back to my code to check possible errors. It turns out the that I should have used “yyyyMMdd” instead of “yyyymmdd” when converting DateTime to string.

Admitted, this bug was due to my lack of attention to details. The difference between “mm” and “MM” are cleared stated in all C# references. I still would like to argue that it’s pretty easy to overlook the differences if one doesn’t work with this kind of tasks everyday.

My question is: Is there a clean(i.e. no magic string) way to do the coverings in one line of code? Thereturn dateTime.Year + "" + dateTime.Month + "" + dateTime.Day; code seems to be working but it’s too much like hacking.

Update: Looks like the string format way is the best C# can offer. Maybe I am being brain washed, but I still think this kind of programming style belongs to low-level languages such as c.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:37:20+00:00Added an answer on May 12, 2026 at 8:37 pm
    String.Format("{0:0000}{1:00}{2:00}", dateTime.Year, dateTime.Month, dateTime.Day);
    

    You could use this instead, I prefer the terse format though. Instead of 00 you can also use MM for specific month formatting (like in DateTime.ToString()).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a org.w3c.dom.Document object to a String. I'm using Java 6
I inherited a project with drupal and flash, and I'm pretty noobish on the
I was asked today by a senior developer which doctype I'd use if I
When a previous Vim session crashed, you are greeted with the Swap file ...
So I installed Entity Framework June CTP day before and all was fine until
So I'm chugging along on this MVC application and all is running smoothly. I
The default string normalization (provided by the Doctrine_Inflector::urlize() sluggifier) isn't suitable for my project,
First post here: -Deep Breath- In order to 'windowise' an app that works pretty
My question is pretty simple: On the iPhone/iPad simulator, when you push a nil
The problem I have a list of tasks with which a user is greeted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.