0

I need to accept either DateTime or Date information from a text file. The challenge is, this date information comes in any format each time. It can be in any format below.

20160131 23:30:15
31032016 23:30:15
2016/01/31 23:30:15
01/31/2016 23:30:15
31/01/2016 23:30:15
31.01.2016 23:30:15
31/01/2016 23:30
// ... more formats come with '-' separators as well and some similar format

How can I unify these random formats into a string or even straight into DateTime ? Or is there any 3rd party library that can handle this scenario?

1
  • The text file does not contain the information needed to communicate the date. Who's throwing it away? Commented Feb 23, 2016 at 5:19

2 Answers 2

2

Use TryParseExact (or ParseExact) and specify all of the format into an array string[] formats

string[] formats = { "yyyyMMdd HH:mm:ss", "ddMMyyyy HH:mm:ss",
                    "yyyy/MM/dd HH:mm:ss", "MM/dd/yyyy HH:mm:ss" }; //and so on, add more
string dtText = "01/31/2016 23:30:15"; //example
DateTime dt;
bool result = DateTime.TryParseExact(dtText, formats, CultureInfo.InvariantCulture, 
    DateTimeStyles.AssumeLocal, out dt);

Once you have this, then you could handle all the specified DateTime formats.


Important

Note that it is impossible for you to unify all possible DateTime formats since some of them are mutually exclusive:

Given an ambiguous case:
"12-12-2012"

and formats:
"dd-MM-yyyy" 
"MM-dd-yyyy"

There can only be one format to be used among the two above.

That's why, it is good for you to customize for each project depending on your need.


If you want this to be reusable and customizable, I recommend you to create a master file (.txt or .xml) which stores all the DateTime formats which you might want to use in your projects.

masterdtformat.txt

"yyyy/MM/dd HH:mm:ss"
"yyyyMMdd HH:mm:ss"
"ddMMyyyy HH:mm:ss"
"MM/dd/yyyy HH:mm:ss"
"dd-MMM-yyyy HH:mm:ss"
 //and so on...

Then, whenever you have a project, you simply need to take some formats from your master file to your particular project file. And in the project, you load that project file to give all the formats you need.

projectdtformat.txt

"yyyy/MM/dd HH:mm:ss"
"yyyyMMdd HH:mm:ss"
//suppose you only need two

This way, whatever format you have specified once, will be reusable. And the mechanism to provide valid DateTime formats for a given project will also be done quite easily by changing the project file.

Sign up to request clarification or add additional context in comments.

Comments

2

DateTime.Parse will take care of almost all of these for you. Try it and see! :-)

If you're worried about different separators, first do a String.Replace of '-' with '/'.

The better question is: how do you handle 07/04/2016? You can't be sure if it's July 4th or April 7th, and you need some kind of feedback to set it one way or the other.

1 Comment

Absolutely, regarding your last sentence. Without the answer to this question, the task is ill defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.