1

I want to convert persia date to Gregorian Date. I use this code.

public static void GetDate()
 {
String persianDate="1384/03/15"
Datetime dt= GetComponentDate(persianDate);
 }

 DateTime GetComponentDate(string fdate)
{
    DateTime edate;
    int year = int.Parse(Persia.Number.ConvertToLatin(fdate.Split('/')[0]));
    int month = int.Parse(Persia.Number.ConvertToLatin(fdate.Split('/')[1]));
    int day = int.Parse(Persia.Number.ConvertToLatin(fdate.Split('/')[2]));
    try
    {
        System.Globalization.PersianCalendar x = new System.Globalization.PersianCalendar();
        DateTime dt = x.ToDateTime(year, month, day, 0, 0, 0, 0, 0);

        return dt;

    }
    catch
    {
    }
    return DateTime.MinValue;
}

In a server this method is runnig good and output is "17/6/2017" . but in a another server this method not work and output="15/03/1384" and I have to use this code after GetComponentDate is used.

((DateTime)dt).ToString(CultureInfo.InvariantCulture);

what is different in two servers? can you help me?

Ftm
  • 99
  • 1
  • 11
  • What does "not work" mean? Can you **also** give some sample input and the associated output of calls to `GetComponentDate`? – Enigmativity Jun 17 '17 at 08:15
  • What is `Persia.DateType.Gregorian`? Calling `ToString` shouldn't do anything on its own as a statement... what is `dt`? There's a lot of information missing here at the moment. – Jon Skeet Jun 17 '17 at 08:15
  • I updated my code. in a server I can convert persian date when I use method GetComponentDate only but in another server I shoud use CultureInfo.InvariantCulture when I want concert date to String. what is different in two servers? – Ftm Jun 17 '17 at 08:18
  • What is `Persia` & `Persia.Number`? You need to provide us with a [mcve]. We need to be able to run your code. – Enigmativity Jun 17 '17 at 08:25
  • It's still not clear what's wrong - I suspect it has nothing to do with the code you've shown us. If you hard-code a `DateTime` value (instead of using that code) do you see the same result? (Note that catching all exceptions like this is a pretty ugly way of handling invalid input, but that's a different matter.) I strongly suspect that one of your servers is using a default culture which uses a non-Gregorian calendar system, and the other isn't. If you want a specific format using a specific calendar, it's entirely reasonable to specify the culture... – Jon Skeet Jun 17 '17 at 08:55
  • For diagnostics purposes, I suggest you print out `Thread.CurrentThread.CurrentCulture` - I'm sure that will show you the difference between the two machines... – Jon Skeet Jun 17 '17 at 08:55
  • @FatemeJafari - Please, we need a [mcve]. Your code should copy-paste-and-run for us. – Enigmativity Jun 17 '17 at 09:30

1 Answers1

0

You say you need to use CultureInfo.InvariantCulture in order to make it work on both servers, and then you ask "what is different in two servers".

I'd say what is different is the culture.

You can set your app culture by adding the globalization line in your web.config :

<configuration>
    <system.web>
        <globalization uiCulture="[uiculture]" culture="[culture]" />

Replace [uiculture] and [culture] with accurate values.

E.g for US culture :

<globalization uiCulture="en-US" culture="en-US"/>

Here is a list of all different cultures.

Rafalon
  • 4,450
  • 2
  • 16
  • 30
  • why in first server I don't use CultureInfo.InvariantCulture and code is running good? how can I set culture in server? – Ftm Jun 17 '17 at 08:45
  • I searched a lot. I use in webconfig but It is not effect in .ascx page also I wrote Thread.CurrentThread.CurrentCulture in Gloabal.ascx but This is not work too – Ftm Jun 17 '17 at 08:50
  • where can I compare cultutre in two servers? – Ftm Jun 17 '17 at 08:51
  • I tested both of them.but it does not any effect – Ftm Jun 17 '17 at 08:53
  • @FatemeJafari Can you copy/paste the exact line you inserted in your web.config ? – Rafalon Jun 17 '17 at 08:55
  • excusme what is accurate values? I don't know. I know culture is different but I don't know where can I see sever's culture – Ftm Jun 17 '17 at 09:00
  • I wrote in webconfig – Ftm Jun 17 '17 at 09:01
  • @FatemeJafari What you want to do in your web.config is set the default culture of your app regardless of the server's culture. See my edit for more info. – Rafalon Jun 17 '17 at 09:04
  • I want to use CultureInfo.InvariantCulture default in web config. I copy/past my code () but not effect in out put! output is "15/03/1384" – Ftm Jun 17 '17 at 09:11
  • @FatemeJafari did you put this line at the right place ? (I edited my answer again) Also is your output still different between servers or does it output "15/03/1384" on both servers ? – Rafalon Jun 17 '17 at 09:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146933/discussion-between-rafalon-and-fateme-jafari). – Rafalon Jun 17 '17 at 09:39
  • please see my chat – Ftm Jun 18 '17 at 06:46