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 1029979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:37:48+00:00 2026-05-16T12:37:48+00:00

This issue stems from an accounting package that spits out text-based reports with rows

  • 0

This issue stems from an accounting package that spits out text-based reports with rows of data having invalid dates, like February 31st or September 31st.

The reports are formatted with spaces and mono-spaced fonts. My goal is to parse out the data needed and generate a more formal report (SSRS).

What I am interested in fixing is the situation where a date is invalid and can’t be directly converted into a DateTime struct. The date format from the report is MMM-dd-yy (e.g. Feb-30-10). I would like to convert the invalid date strings into the closest valid DateTime in the same month before showing them in the formal report. I’ve seen this done two ways in my time as a developer, both very poorly, so I want to come up with a simple way of doing it (if there isn’t a built-in way I don’t know about).

The first bad method I’ve seen(I can’t believe I’m even showing you!):

Dim month As Integer = <Parse out the month from the bad date string>
Dim day As Integer = <Parse out the day from the bad date string>
Dim year As Integer = <Parse out the year from the bad date string>

Dim validDate As DateTime

While True
    Try
        validDate = New DateTime(year, month, day)
        Exit While
    Catch ex As ArgumentOutOfRangeException
        day -= 1
    End Try
End While

I hope I don’t have to explain what I don’t like about that method.

The second bad method:

Dim badDateString As String = <Current date string from text report>
Dim validDate As DateTime

If DateTime.TryParseExact(badDateString, "MMM-dd-yy", Nothing, Globalization.DateTimeStyles.None, validDate) Then
    Return validDate
End If

badDateString = badDateString.Replace("31", "30")

' ... try the parse again, if still not valid, replace "30" with "29"
' ... try the parse again, if still not valid, replace "29" with "28"

These make for some sad code and me a sad developer.

I’ve been trying to think of a more efficient way of doing this. Any ideas?

EDIT:

I found a solution and have posted it, but I liked Guffa’s answer more.

  • 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-16T12:37:48+00:00Added an answer on May 16, 2026 at 12:37 pm

    Reading the previous code, the last code is pretty much what I was going to suggest.

    Here is a variation of the code:

    Return New DateTime(year, month, Math.Min(day, DateTime.DaysInMonth(year, month)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a long time I've been having this issue that at a certain time
Ok this question stems from this question: wp7: App failing! Can not figure out
It turns out this whole misunderstanding of the open() versus fopen() stems from a
I've been investigating this issue that only seems to get worse the more I
I have an issue that seems like very flaky behavour, is this a problem
It seems (at least that is our understanding of the issue at this point)
I am having an issue where textures from the resources become just white. The
This question stems from watching Rasmus Lerdorf's talk from Drupalcon . This question and
Can anyone recreate this issue? It seems to me like a quite serious bug
I have read a lot about this issue but mine still seems to be

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.