Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a datetime in CurrentCulture format to Gregorian DateTime?

Tags:

c#

datetime

I set Global.asax as this:

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        PersianCulture fa = new PersianCulture();

        Thread.CurrentThread.CurrentCulture = fa;
    }

I want to convert "1392 1 23" to "2013 4 12".

How can I do that?

like image 996
Majid Avatar asked Apr 12 '13 05:04

Majid


People also ask

How do I convert a DateTime to a specific format?

The ToString() method of the DateTime class is used to convert a DateTime date object to string format. The method takes a date format string that specifies the required string representation.

What is a Gregorian date format?

YYYY/MM/DD - Sortable style Gregorian date format.

What is T and Z in time format?

What is T between date and time? The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC). If your strings always have a “Z” you can use: SimpleDateFormat format = new SimpleDateFormat( “yyyy-MM-dd'T'HH:mm:ss).


1 Answers

There's no such thing as "a Persian DateTime". A DateTime value is always a Gregorian calendar value, with no specific formatting. When you format it (usually by calling ToString) you can determine how it's formatted - and if you use a culture which uses a non-Gregorian calendar, it will convert the original value into that calendar.

So for example, if you wanted to parse input from a Persian user and then convert that to the equivalent date that an English user would understand, you could use:

DateTime date = DateTime.Parse(text, persianCulture);
string englishText = date.ToString(englishCulture);
like image 80
Jon Skeet Avatar answered Oct 02 '22 12:10

Jon Skeet