Ehsan Akbar
Ehsan Akbar

Reputation: 7301

Split string to convert to datetime causes an error

I try to convert persian date to standard datetime .The persian date has a format like this :1392/01/23.

My function :

 public DateTime ConvertPeersianToEnglish(string persianDate)
        {
            string[] formats = { "yyyy/MMMM/dd" };
            DateTime d1 = DateTime.ParseExact(persianDate, formats,
                                              CultureInfo.CurrentCulture, DateTimeStyles.None);

            return d1;
        }

So when i call this function and pass my persian date to it i got an error :

String was not recognized as a valid DateTime.

Why ?

Best regards

Upvotes: 0

Views: 84

Answers (1)

Zbigniew
Zbigniew

Reputation: 27584

Use MM instead of MMMM:

string[] formats = { "yyyy/MM/dd" };

MM is month number 01 to 12

MMMM is full month name january to december (strings depend on culture).

Check out MSDN: Custom date and time format strings

Upvotes: 4

Related Questions