CSharpBeginner
CSharpBeginner

Reputation: 611

Get the date format of given date string C#

I'm using C#.

I'm looking for a way to know the given date format I.e if i have the following date: 2016-11-17T21:00:00.180-03:00 I'm excepted to yyyy-MM-dd'T'HH:mm:ss.ffZ and if i have the following date 2016-17-11T21:00:00.180-03:00 I'm excepted to yyyy-dd-MM'T'HH:mm:ss.ffZ etc..

What is the best way to do that?

Upvotes: 0

Views: 443

Answers (1)

Christos
Christos

Reputation: 53958

You can't get the format of a date as simple as calling a method or a property of a DateTime object.

However, it could be useful in your case the DateTime.TryParseExact method, which

Converts the specified string representation of a date and time to its DateTime equivalent. The format of the string representation must match a specified format exactly. The method returns a value that indicates whether the conversion succeeded.

So, if you do this for two or more separate formats, you could validate that your string has one of the formats you are looking for or not.

For further info regarding the DateTime.TryParseExact method please have a look here.

Upvotes: 2

Related Questions