9

I have searched stackoverflow for an answer but no luck. I am developing a windows application and I have some strings in different date formats, eg.

dd/MM/yyyy
MM/dd/yyyy
MM-dd-yyyy
dd-MM-yyyy
dd/MM/yyyy hh:mm::ss
MM/dd/yyyy hh:mm::ss
etc...

But I need to convert in to a common format - dd/MM/yyyy. The application can run in any windows machines in different culture.

What is the correct way to do it?

EDIT: One more thing I may not know what the format of incoming string.

Thanks in advance.

4
  • r u looking for something like this? csharp-examples.net/string-format-datetime Commented Jul 10, 2012 at 7:18
  • you can use Date.Parse("dd/MM/yyyy"); Commented Jul 10, 2012 at 7:19
  • 1
    When you say you have "strings in different date formats" - is there any factor that determines (or at least suggests) what that date format will be? As an aside - what you should be aiming to do is convert them to a DateTime (and to store them as a DateTime) formatting is then just a presentation issue. Commented Jul 10, 2012 at 7:21
  • 1
    Your seperating characters shouldn't be too much of an issue and should mostly just work. But not knowing the order of the day and the month is going to be your problems. If I present the date "04/06/2012" then is this the 4th June or the 6th April? Without something telling you which way around the date parts are you can only take a best guess. Ideally your input dates would be in an international format like yyyy-MM-dd HH:mm:ss Commented Jul 10, 2012 at 7:21

4 Answers 4

8

Use DateTime.ParseExact with the different patterns as formats.

If after parsing you really need to use a string representation, use the ToString method of the DateTime with the explicit format that you're interested in (so that it is culture-invariant). It's better however to keep the DateTime because this is format-agnostic.

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

8 Comments

I saw it. But I donot know what is the format of in coming string. Anyway?
The program should automatically detect the incoming format and convert it to dd/MM/yyyy
I posted a link (just click on the method name) - there is an example doing just what you're asking for.
How is ParseExact going to help?
@AMissico, because if the format is not known - the only way to do this is to try and parse it using a bunch of known formats until it succeeds, or you run out of formats.
|
3

You could distinguish between those formats that use different separators (i.e. "/" vs "-"). But how would you know if date such as 10/11/2010 represents 10th of November or 11th of October? If one number is not bigger than 12, there is no reliable way to do this without knowing an exact format.

As others have pointed out, if you do know the exact format, then you can use DateTime.ParseExact.

Comments

0

If you are processing some import file with a lot of dates in the same unknown format, you could try different formats and hope there is exactly one that doesn't give format errors.

Or to put it another way: split the "dates" into three numbers and check the range of values for each of those numbers. Values > 1900 will be years. If you find values from 1 to 31, those will be days. Values from 1 to 12 might be months, but could also be days. Try and identify each of the parts.

The best way is to ask the supplier of those dates for the format.

Comments

0

To run this program on different culture, i think you should creat a function to indentify the culture of this string format and then use Datetime.Parse

1 Comment

And how would you do that? If "04/06/2012" comes, how will you determine that the format is dd/mm/yyyy or mm/dd/yyyy? You need to develop more your answers... and to think them a little more. Welcome on SO! ;)

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.