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.
Use
DateTime.ParseExactwith the different patterns as formats.If after parsing you really need to use a string representation, use the
ToStringmethod of theDateTimewith the explicit format that you’re interested in (so that it is culture-invariant). It’s better however to keep theDateTimebecause this is format-agnostic.