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

  • Home
  • SEARCH
  • 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 533961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:33:45+00:00 2026-05-13T09:33:45+00:00

I have a method that (sometimes) takes in a string in the format dddd

  • 0

I have a method that (sometimes) takes in a string in the format "dddd MMMM dd" (Monday January 04) that needs to get parsed into a DateTime. I say sometimes because it may also get passed in "Today" or "Tomorrow" as the value.

The code to handle this was simple enough:

if (string.Compare(date, "Today", true) == 0)
    _selectedDate = DateTime.Today;
else if (string.Compare(date, "Tomorrow", true) == 0)
    _selectedDate = DateTime.Today.AddDays(1);
else
    _selectedDate = DateTime.Parse(date);

This worked until halfway through December. Some of you have probably already spotted what went wrong.

This would have failed on any date in the New Year with the error:

“String was not recognized as a valid DateTime because the day of week was incorrect.”

It was getting passed "Monday January 04" which is a valid date for 2010, but not in 2009.

So my question is: Is there any way to set the year either for the current year or the next year? Right now, as a quick and dirty fix, I have this:

if (!DateTime.TryParseExact(date, "dddd MMMM dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out _selectedDate))
    if (!DateTime.TryParseExact(date + " " + (DateTime.Now.Year + 1), "dddd MMMM dd yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out _selectedDate))
        throw new FormatException("That date is not valid.");

So it will try to parse it using the current year, and if it is unsuccessful it will try again using the next year. If it fails after that, it’ll just assume it’s an invalid date because I only need to worry about 1 year in advance, but if anyone has a more flexible solution, I’d appreciate it. (Note, I don’t need to worry about validating the date that gets passed in, it will be valid for either the current or following year).

  • 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-13T09:33:45+00:00Added an answer on May 13, 2026 at 9:33 am

    First, your unit testing should have caught this. You might want to revisit the tests that you wrote for this method to learn from this experience on how to cover your functionality more throughly.

    Second, is there any particular reason why you are using String.Compare instead of String.Equals? I consider the following more readable:

    date.Equals("Today", StringComparison.InvariantCultureIgnoreCase);
    

    I think it more clearly reads what is happening (especially as we don’t have to remember what the final bool parameter means in String.Compare).

    Now, to get the heart of your question. Your method is perfectly fine and very clearly expresses the logic. I would make one small refactoring however:

    public DateTime ParseInThisYearOrNextYear(string s, out DateTime dt)
    {
        if (!Parse(s, "dddd MM dd", out dt))
        {
            if (!Parse(s + " " + DateTime.Now.Year + 1, "dddd MM dd yyyy", out dt))
            {
                throw new FormatException();
            }
        }
    
        return dt;
    }
    
    bool Parse(string s, string format, out DateTime dt)
    {
        return DateTime.TryParseExact(
            s,
            format,
            CultureInfo.InvariantCulture,
            DateTimeStyles.None,
            out dt
        );
    }
    

    This separates your method into two distinct pieces of functionality and prevents from repeating yourself (CultureInfo.InvariantCulture and DateTimeStyles.None) making testing and maintenance a little easier. (You probably want a better method name than Parse; I chose a short one to prevent the scroll bar from appearing in the code window here.)

    As one last caveat (without knowing the details of your system) you might want to consider also checking the prior year! Just imagine the following situation:

    1. Input is “Thursday December 31” (valid for 2009).
    2. System rolls over January 1 boundary into 2010.
    3. Code is executed and checks 2010 and 2011 both of which fail.

    Just something to consider depending on the nature of your system.

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

Sidebar

Ask A Question

Stats

  • Questions 299k
  • Answers 299k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Django's template engine is not capable of making this change.… May 13, 2026 at 7:51 pm
  • Editorial Team
    Editorial Team added an answer Change the markup to something like <input type="checkbox" class='form' value="1"… May 13, 2026 at 7:51 pm
  • Editorial Team
    Editorial Team added an answer You can search for matching id2's, and use count(distinct id2)… May 13, 2026 at 7:51 pm

Related Questions

My application produces an xml file that is then xslt transformed into a nice
Yet another one of my idiotic P/Invoke questions... Sometimes I think this stuff is
So I have this code that takes care of command acknowledgment from remote computers,
I'm working on an asp.net MVC application. I have a class that wraps a
I have two independant threads F1 and F2 (to be precise, two instances of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.