amin amin - 25 days ago 6
ASP.NET (C#) Question

how can i show correctly datetime in C#

i have a time stamp in Persian Date Format and i want to convert to full Persian date, my code work correctly in localhost but when upload it to remote server output changed to Gregorian date

string dt = "";
PersianCalendar pc = new PersianCalendar();
int Year = Convert.ToInt32(ts.Substring(0, 4));
int Month = Convert.ToInt32(ts.Substring(4, 2));
int Day = Convert.ToInt32(ts.Substring(6, 2));
int Hour = Convert.ToInt32(ts.Substring(8, 2));
int Minute = Convert.ToInt32(ts.Substring(10, 2));
int Second = Convert.ToInt32(ts.Substring(12, 2));

dt = new DateTime(Year, Month, Day, Hour, Minute, Second, 0, pc).ToString("dddd dd MMMM yyyy HH:mm:ss", CultureInfo.CreateSpecificCulture("fa-IR"));


output this code that i expect is (in localhost):


شنبه 22 آبان 1395 13:43:10


and output in remote server


شنبه 12 نوامبر 2016 13:43:10


what should i do it work correctly?

Answer

The calendar parameter in DateTime constructor is used to interpret integer values not to dictate the result of the calendar. The code you have written is culture dependent which is not a good practice in Iran since we don't use correct culture in our systems. My advice would be to use something like this:

public string GetPersianDateString(DateTime date)
{
    PersianCalendar pc = new PersianCalendar();
    string dayOfWeek;
    switch (pc.GetDayOfWeek(date))
    {
        case DayOfWeek.Sunday:
            dayOfWeek = "یکشنبه";
            break;
        case DayOfWeek.Monday:
            dayOfWeek = "دوشنبه";
            break;
        case DayOfWeek.Tuesday:
            dayOfWeek = "سه شنبه";
            break;
        case DayOfWeek.Wednesday:
            dayOfWeek = "چهارشنبه";
            break;
        case DayOfWeek.Thursday:
            dayOfWeek = "پنج شنبه";
            break;
        case DayOfWeek.Friday:
            dayOfWeek = "جمعه";
            break;
        case DayOfWeek.Saturday:
            dayOfWeek = "شنبه";
            break;
        default:
            dayOfWeek = "";
            break;
    }
    var day = pc.GetDayOfMonth(date).ToString();
    string month;
    switch (pc.GetMonth(date))
    {
        case 1:
            month = "فروردین";
            break;
        case 2:
            month = "اردیبهشت";
            break;
        case 3:
            month = "خرداد";
            break;
        case 4:
            month = "تیر";
            break;
        case 5:
            month = "مرداد";
            break;
        case 6:
            month = "شهریور";
            break;
        case 7:
            month = "مهر";
            break;
        case 8:
            month = "آبان";
            break;
        case 9:
            month = "آذر";
            break;
        case 10:
            month = "دی";
            break;
        case 11:
            month = "بهمن";
            break;
        case 12:
            month = "اسفند";
            break;
        default:
            month = "";
            break;
    }
    var year=pc.GetYear(date).ToString();
    return dayOfWeek + " " + day + " " + month + " " + year + " " + date.ToShortTimeString();
}

This is just a draft and you can write better organized code.