Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform between datetime formats

Tags:

date

c#

datetime

I am facing a problem in which I need to transform dates in a given input format into a target one. Is there any standard way to do this in C#?

As an example say we have yyyy.MM.dd as the source format and the target format is MM/dd/yyy (current culture).

The problem arises since I am using a parsing strategy that gives priority to the current culture and then if it fails it tries to parse from a list of known formats. Now say we have two equivalent dates one in the source culture above (2015.12.9) and the other in the current culture (9/12/2015). Then if we attempt to parse this two dates the month will be 12 for the first case and in the second will be 9, so we have an inconsistency (they were supposed to mean be the same exact date).

I believe that if existing it should be something as

DateTime.Convert(2015.12.9, 'yyyy/MM/dd', CultureInfo.CurrentCulture). 

Any ideas?

EDIT:

Thank you all for your ideas and suggestions, however the interpretation most of you gave to my question was not quite right. What most of you have answered is a direct parse in the given format and then a conversion to the CurrentCulture.

DateTime.ParseExact("2015.12.9", "yyyy.MM.dd", CultureInfo.CurrentCulture)

This will still return 12 as month, although it is in the CurrentCulture format. My question thus was, is there any standard way to transform the date in yyyy.MM.d to the format MM/dd/yyy so that the month is now in the correct place and THEN parsed it in the target culture. Such function is likely to be unexisting.

like image 863
user3853059 Avatar asked Dec 09 '15 10:12

user3853059


2 Answers

DateTime.ParseExact is what you are looking for:

DateTime parsedDate = DateTime.ParseExact("2015.12.9", "yyyy.MM.d", CultureInfo.InvariantCulture);

Or eventualy DateTime.TryParseExact if you're not confident with input string.

like image 75
Perfect28 Avatar answered Oct 13 '22 12:10

Perfect28


I know it's late but I try to explain little bit deep if you let me..

I am facing a problem in which I need to transform dates in any format to a target one.

There no such a thing as dates in any format. A DateTime does not have any implicit format. It just has date and time values. Looks like you have a string which formatted as date and you want to convert another string with different format.

Is there any standard way to do this in C#?

Yes. You can parse your string with DateTime.ParseExact or DateTime.TryParseExact first with specific format to DateTime and then generate it's string representation with a different format.

As an example say we have yyyy.MM.dd as the source format and the target format is MM/dd/yyy (current culture).

I didn't understand what is the meaning of current culture in this sentences and I assume you want yyyy not yyy, but you can generate it as I described above like;

string source = "2015.12.9";
DateTime dt = DateTime.ParseExact(source, "yyyy.MM.d", CultureInfo.InvariantCulture);
string target = dt.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture); // 12/09/201

The problem arises since I am using a parsing strategy that gives priority to the current culture and then if it fails it tries to parse from a list of known formats.

Since you didn't show any parsing strategy and there is no DateTime.Convert method in .NET Framework, I couldn't any comment.

Now say we have two equivalent dates one in the source culture above (2015.12.9) and the other in the current culture (9/12/2015). Then if we attempt to parse this two dates the month will be 12 and in the second will be 9, so we have an inconsistency.

Again.. You don't have DateTime's. You have strings. And those formatted strings can't belong on any culture. Sure all cultures might parse or generate different string representations with the same format format a format does not belong any culture.

I assume you have 2 different string which different formatted and you wanna parse the input no matter which one it comes. In such a case, you can use DateTime.TryParseExact overload that takes string array for all possible formats as a parameter. Then generate it's string representation with MM/dd/yyy format and a culture that has / as a DateSeparator like InvariantCulture.

string s = "2015.12.9"; // or 9/12/2015
string[] formats = { "yyyy.MM.d", "d/MM/yyyy" };
DateTime dt;
if (DateTime.TryParseExact(s, formats, CultureInfo.InvariantCulture,
                           DateTimeStyles.None, out dt))
{
    Console.WriteLine(dt.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture));
}
like image 40
Soner Gönül Avatar answered Oct 13 '22 13:10

Soner Gönül