我有一个波斯语日期格式的时间戳,我想转换为完整的波斯语日期,我的代码在localhost中正常工作但是当它上传到远程服务器输出时更改为公历日期
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"));
输出我期望的代码(在localhost中):
شنبه22آبان1395 13:43:10
并在远程服务器中输出
شنبه12نوامبر2016 13:43:10
我该怎么做才能正常工作?
答案 0 :(得分:1)
calendar
构造函数中的DateTime
参数用于解释整数值,而不是指示日历的结果。您编写的代码依赖于文化,这在伊朗不是一个好习惯,因为我们在系统中没有使用正确的文化。我的建议是使用这样的东西:
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();
}
这只是一个草稿,您可以编写更好的组织代码。