Created
May 1, 2019 06:45
-
-
Save khalili-mahdi/b54ff1d3ca59d28bde2f486dbaf597e3 to your computer and use it in GitHub Desktop.
Date Converter Shamsi-Georgian (persian time converter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class CDate | |
{ | |
public static int ToDay_Special_Year(int SAll1) | |
{ | |
DateTime ps1; | |
ps1 = DateTime.Now; | |
int FYear = ps1.Year; | |
int FMonth = ps1.Month; | |
int FDay = ps1.Day; | |
int FDow = (int)(ps1.DayOfWeek); | |
Get_Date_Shams(ref FYear, ref FMonth, ref FDay, ref FDow); | |
//int FYear2 = FYear - 1300; | |
if (SAll1 < 20) SAll1 += 1400; | |
else if (SAll1 < 100) SAll1 += 1300; | |
return SAll1 * 10000 + FMonth * 100 + FDay; | |
} | |
public static int Transfer_Date(int date, int rooz) | |
{ | |
int Year = 0, Month = 0, Day = 0; | |
Year = (int)date / 10000; | |
Month = (int)((date - Year * 10000) / 100); | |
Day = date - Year * 10000 - Month * 100; | |
if (rooz > 0) | |
{ | |
int d_y = (rooz / 365); | |
Year += d_y; | |
rooz = (rooz % 365); | |
int d_m = (rooz / 30); | |
Month += d_m; | |
if (Month > 12) | |
{ | |
Month -= 12; | |
Year++; | |
} | |
rooz = (rooz % 30); | |
int d_r = rooz; | |
Day += d_r; | |
if ((Month < 7) && (Day > 31)) | |
{ | |
Day -= 31; | |
Month++; | |
} | |
if ((Month > 6) && (Day > 30)) | |
{ | |
Day -= 30; | |
Month++; | |
} | |
if (Month > 12) | |
{ | |
Month -= 12; | |
Year++; | |
} | |
} | |
else | |
{ | |
rooz = -rooz; | |
int d_y = (rooz / 365); | |
Year -= d_y; | |
rooz = (rooz % 365); | |
int d_m = (rooz / 30); | |
Month -= d_m; | |
if (Month < 1) | |
{ | |
Month += 12; | |
Year--; | |
} | |
rooz = (rooz % 30); | |
int d_r = rooz; | |
Day -= d_r; | |
if (Day < 1) | |
{ | |
Month--; | |
if (Month < 7) Day += 31; | |
else Day += 30; | |
} | |
} | |
return ((int)Year * 10000 + (int)Month * 100 + (int)Day); | |
} | |
public static int GetIntnow() | |
{ | |
return GetIntnow(0); | |
} | |
public static int GetIntnow(int mynow) | |
{ | |
int Result = 0; | |
if (mynow == 0) Result = (int)(((int)DateTime.Now.ToOADate() - 29220) * 86400); | |
else Result = (int)((mynow - 29220) * 86400); | |
return Result; | |
} | |
public static int ToInt(DateTime d1) | |
{ | |
return (int)Convert.ToDouble(d1.ToOADate()); | |
} | |
public static int Date_Diff_To_Day(int FirstDate, int LastDate) | |
{ | |
if (FirstDate < 13000000) FirstDate += 13000000; | |
if (LastDate < 13000000) LastDate += 13000000; | |
return Date_To_Day(LastDate) - Date_To_Day(FirstDate); | |
} | |
//{******************************************************************************} | |
//{******************************************************************************} | |
public static DateTime Change_Shamsi_To_Miladi(ref int sDate) | |
{ | |
int sYear, sMonth, sDay, k_k, x_x, e_e, l_l, w_w, kabise; | |
sDay = sDate % 100; | |
sDate = sDate / 100; | |
sMonth = sDate % 100; | |
sDate = sDate / 100; | |
sYear = sDate % 10000; | |
if ((sDay == 0) && (sMonth == 0) && (sYear == 0)) | |
{ | |
return DateTime.Now; | |
} | |
if (sYear < 100) | |
{ | |
if (sYear > 20) sYear = sYear + 1300; | |
else sYear = sYear + 1400; | |
} | |
k_k = 1074; | |
x_x = 1; | |
w_w = ((365 * (sYear - k_k) + (int)((8 * (sYear - k_k)) / 33) + 31 * (sMonth - 1) - (int)(sMonth / 7) * (sMonth % 7) + sDay + x_x) % 7); | |
l_l = (int)(((8 * (sYear - k_k + 1)) / 33)) - (int)(((8 * (sYear - k_k)) / 33)); | |
if (w_w == 7) w_w = 0; | |
if (!((l_l == 0) && (sMonth == 12) && (sDay == 30))) | |
{ | |
switch (sMonth) | |
{ | |
case 1: if (sDay < 12) { sMonth = 3; sDay = sDay + 20; } else { sMonth = 4; sDay = sDay - 11; } break; | |
case 2: if (sDay < 11) { sMonth = 4; sDay = sDay + 20; } else { sMonth = 5; sDay = sDay - 10; } break; | |
case 3: if (sDay < 11) { sMonth = 5; sDay = sDay + 21; } else { sMonth = 6; sDay = sDay - 10; } break; | |
case 4: if (sDay < 10) { sMonth = 6; sDay = sDay + 21; } else { sMonth = 7; sDay = sDay - 9; } break; | |
case 5: if (sDay < 10) { sMonth = sMonth + 2; sDay = sDay + 22; } else { sMonth = sMonth + 3; sDay = sDay - 9; } break; | |
case 6: if (sDay < 10) { sMonth = sMonth + 2; sDay = sDay + 22; } else { sMonth = sMonth + 3; sDay = sDay - 9; } break; | |
case 8: if (sDay < 10) { sMonth = sMonth + 2; sDay = sDay + 22; } else { sMonth = sMonth + 3; sDay = sDay - 9; } break; | |
case 7: if (sDay < 9) { sMonth = 9; sDay = sDay + 22; } else { sMonth = 10; sDay = sDay - 8; } break; | |
case 9: if (sDay < 10) { sMonth = 11; sDay = sDay + 21; } else { sMonth = 12; sDay = sDay - 9; } break; | |
case 10: if (sDay < 11) { sMonth = 12; sDay = sDay + 21; } else { sMonth = 1; sDay = sDay - 10; sYear++; } break; | |
case 11: if (sDay < 12) { sMonth = 1; sDay = sDay + 20; sYear++; } else { sMonth = 2; sDay = sDay - 11; sYear++; } break; | |
case 12: | |
{ | |
kabise = (DateTime.IsLeapYear(sYear + 622) ? 366 : 365); | |
if (kabise == 365) | |
{ | |
if (sDay < 10) { sMonth = 2; sDay = sDay + 19; sYear++; } else { sMonth = 3; sDay = sDay - 9; sYear++; } | |
} | |
else | |
{ | |
if (sDay <= 10) { sMonth = 2; sDay = sDay + 19; sYear++; } else { sMonth = 3; sDay = sDay - 9; sYear++; } | |
} | |
break; | |
} | |
} | |
} | |
sYear = sYear + 621; | |
e_e = ((int)EncodeDate(sYear, sMonth, sDay).DayOfWeek) + 1; | |
if (e_e == 7) e_e = 0; | |
if ((w_w < e_e) || ((w_w == 6) && (e_e == 0))) | |
{ | |
sDay = sDay - 1; | |
w_w = e_e; | |
if (((sMonth >= 1) && (sMonth <= 6) && (sDay > 31)) || ((sMonth >= 7) && (sMonth <= 11) && (sDay > 30))) | |
{ | |
sDay = 1; | |
sMonth = sMonth + 1; | |
} | |
else if ((sMonth == 12) && (((sDay > 30) && (l_l == 1)) || ((sDay > 29) && (l_l == 0)))) | |
{ | |
} | |
} | |
if ((w_w > e_e) || ((w_w == 0) && (e_e == 6))) | |
{ | |
sDay = sDay - 1; | |
if ((sMonth >= 2) && (sMonth <= 7) && (sDay == 0)) | |
{ | |
sDay = 31; | |
sMonth = sMonth - 1; | |
if ((sMonth >= 8) && (sMonth <= 12) && (sDay == 0)) | |
{ | |
sDay = 30; | |
sMonth = sMonth - 1; | |
} | |
else if ((sMonth == 1) && (sDay == 0)) | |
{ | |
sYear = sYear - 1; | |
l_l = (int)(((8 * (sYear - k_k + 1)) / 33)) - (int)(((8 * (sYear - k_k)) / 33)); | |
if (l_l == 0) sDay = 29; | |
else sDay = 30; | |
sMonth = 12; | |
} | |
} | |
} | |
if (sMonth > 12) | |
{ | |
sYear = sYear + 1; | |
sMonth = sMonth - 12; | |
} | |
if (sDay == 0) | |
{ | |
sMonth--; | |
if (sMonth == 0) | |
{ | |
sYear--; | |
sMonth = 12; | |
} | |
switch (sMonth) | |
{ | |
case 1: | |
case 3: | |
case 5: | |
case 7: | |
case 8: | |
case 10: | |
case 12: | |
sDay = 31; | |
break; | |
case 2: | |
sDay = 28; | |
break; | |
case 4: | |
case 6: | |
case 9: | |
case 11: | |
sDay = 30; | |
break; | |
default: | |
sDay = 0; | |
break; | |
} | |
} | |
if (sMonth == 0) | |
{ | |
sYear--; | |
sMonth = 12; | |
} | |
if (sDay == 0) | |
{ | |
sMonth--; | |
switch (sMonth) | |
{ | |
case 1: | |
case 3: | |
case 5: | |
case 7: | |
case 8: | |
case 10: | |
case 12: sDay = 31; break; | |
case 2: sDay = 28; break; | |
case 4: | |
case 6: | |
case 9: | |
case 11: sDay = 30; break; | |
default: | |
sDay = 0; break; | |
} | |
} | |
if (sMonth == 0) | |
{ | |
sYear--; | |
sMonth = 12; | |
} | |
sDate = sYear * 10000 + sMonth * 100 + sDay; | |
return EncodeDate(sYear, sMonth, sDay); | |
} | |
public static int CalcTime(int time1, int time2, char oper) | |
{ | |
int H1, H2, M1, M2, S1, S2, H, M, S; | |
//int Result = 0; | |
if ((time1 <= time2) && (oper == '-')) | |
{ | |
return 0; | |
} | |
S1 = time1 % 100; | |
M1 = (time1 / 100) % 100; | |
H1 = (time1 / 10000) % 100; | |
S2 = time2 % 100; | |
M2 = (time2 / 100) % 100; | |
H2 = (time2 / 10000) % 100; | |
switch (oper) | |
{ | |
case '+': | |
{ | |
S = (S1 + S2) % 60; | |
M = (M1 + M2 + (S1 + S2) / 60) % 60; | |
H = H1 + H2 + (M1 + M2 + (S1 + S2) / 60) / 60; | |
} | |
break; | |
case '-': | |
{ | |
if (S1 < S2) | |
{ | |
S1 += 60; | |
M1--; | |
} | |
if (M1 < M2) | |
{ | |
M1 += 60; | |
H1--; | |
} | |
H = H1 - H2; | |
M = M1 - M2; | |
S = S1 - S2; | |
} | |
break; | |
default: | |
{ | |
H = 0; | |
M = 0; | |
S = 0; | |
} | |
break; | |
} | |
return H * 10000 + M * 100 + S; | |
} | |
public static int Time_Diff(int time1, int time2) | |
{ | |
return CalcTime(time1, time2, '-'); | |
} | |
public static int TimePlus(int time1, int time2) | |
{ | |
return CalcTime(time1, time2, '+'); | |
} | |
public static int Date_To_Day(int date) | |
{ | |
int y = (date / 10000); | |
int m = ((date % 10000) / 100); | |
int d = (date % 100); | |
int sum = 0; | |
int i; | |
for (i = 1; i < y; i++) sum += 365; | |
for (i = 1; i < m; i++) sum += 31; | |
sum += d; | |
sum -= (m - 6); | |
return sum; | |
} | |
//public static int Get_Date_Sabt(TSESSION ses1, int cur_date_sabt) | |
//{ | |
// if (cur_date_sabt == 0) | |
// { | |
// if (CDate.TwoDigitsYear() == ses1.User.i_pish) cur_date_sabt = CDate.ToDay(); | |
// else | |
// { | |
// if (TAPP1.END_ROZ_SALL == 0) cur_date_sabt = 13000000 + (ses1.User.i_pish + 1) * 10000 + 101; | |
// else cur_date_sabt = 13000000 + ses1.User.i_pish * 10000 + 1200 + TAPP1.END_ROZ_SALL; | |
// } | |
// } | |
// return cur_date_sabt; | |
//} | |
public static void Get_Date_Milad(ref ushort Year, ref ushort Month, ref ushort Day, ref ushort Dow) | |
{ | |
int e_e, x_x, l_l, k_k, y_y, m_m, d_d; | |
y_y = Year; | |
m_m = Month; | |
d_d = Day; | |
if (y_y < 100) | |
{ | |
if (y_y > 20) y_y = y_y + 1300; | |
else y_y = y_y + 1400; | |
} | |
k_k = 1074; | |
x_x = 1; | |
e_e = ((365 * (y_y - k_k) + (int)(((8) * (y_y - k_k) / 33)) + 31 * (m_m - 1) - (int)(m_m / 7) * (m_m % 7) + d_d + x_x) % 7); | |
//if e_e=0 e_e= 7; | |
y_y = y_y + 621; | |
l_l = 1;//Round((8*(y_y-k_k+1))/ 33)-Round((8*(y_y-k_k))/ 33); | |
if (!((l_l == 0) && (m_m == 12) && (d_d == 30))) | |
{ | |
if ((m_m == 1) && (d_d >= 1) && (d_d <= 11)) | |
{ | |
m_m = m_m + 2; | |
d_d = d_d + 20; | |
} | |
else if ((m_m == 1) && (d_d >= 12) && (d_d <= 31)) | |
{ | |
m_m = m_m + 3; | |
d_d = d_d - 11; | |
} | |
else if ((m_m == 2) && (d_d >= 1) && (d_d <= 10)) | |
{ | |
m_m = m_m + 2; | |
d_d = d_d + 20; | |
} | |
else if ((m_m == 2) && (d_d >= 11) && (d_d <= 31)) | |
{ | |
m_m = m_m + 3; | |
d_d = d_d - 10; | |
} | |
else if ((m_m == 3) && (d_d >= 1) && (d_d <= 10)) | |
{ | |
m_m = m_m + 2; | |
d_d = d_d + 20; | |
} | |
else if ((m_m == 3) && (d_d >= 11) && (d_d <= 31)) | |
{ | |
m_m = m_m + 3; | |
d_d = d_d - 10; | |
} | |
else if ((m_m == 4) && (d_d >= 1) && (d_d <= 9)) | |
{ | |
m_m = m_m + 2; | |
d_d = d_d + 21; | |
} | |
else if ((m_m == 4) && (d_d >= 10) && (d_d <= 31)) | |
{ | |
m_m = m_m + 3; | |
d_d = d_d - 9; | |
} | |
else if ((m_m == 5) && (d_d >= 1) && (d_d <= 9)) | |
{ | |
m_m = m_m + 2; | |
d_d = d_d + 22; | |
} | |
else if ((m_m == 5) && (d_d >= 10) && (d_d <= 31)) | |
{ | |
m_m = m_m + 3; | |
d_d = d_d - 9; | |
} | |
else if ((m_m == 6) && (d_d >= 1) && (d_d <= 9)) | |
{ | |
m_m = m_m + 2; | |
d_d = d_d + 22; | |
} | |
else if ((m_m == 6) && (d_d >= 10) && (d_d <= 31)) | |
{ | |
m_m = m_m + 3; | |
d_d = d_d - 9; | |
} | |
else if ((m_m == 7) & (d_d >= 1) && (d_d <= 8)) | |
{ | |
m_m = m_m + 2; | |
d_d = d_d + 22; | |
} | |
else if ((m_m == 7) && (d_d >= 9) && (d_d <= 30)) | |
{ | |
m_m = m_m + 3; | |
d_d = d_d - 8; | |
} | |
else if ((m_m == 8) && (d_d >= 1) && (d_d <= 9)) | |
{ | |
m_m = m_m + 2; | |
d_d = d_d + 22; | |
} | |
else if ((m_m == 8) && (d_d >= 10) && (d_d <= 30)) | |
{ | |
m_m = m_m + 3; | |
d_d = d_d - 9; | |
} | |
else if ((m_m == 9) && (d_d >= 1) && (d_d <= 9)) | |
{ | |
m_m = m_m + 2; | |
d_d = d_d + 21; | |
} | |
else if ((m_m == 9) && (d_d >= 10) && (d_d <= 30)) | |
{ | |
m_m = m_m + 3; | |
d_d = d_d - 9; | |
} | |
else if ((m_m == 10) && (d_d >= 1) && (d_d <= 10)) | |
{ | |
m_m = m_m + 2; | |
d_d = d_d + 21; | |
} | |
else if ((m_m == 10) && (d_d >= 11) && (d_d <= 30)) | |
{ | |
m_m = m_m + 3; | |
d_d = d_d - 10; | |
} | |
else if ((m_m == 11) && (d_d >= 1) && (d_d <= 11)) | |
{ | |
m_m = m_m + 2; | |
d_d = d_d + 20; | |
} | |
else if ((m_m == 11) && (d_d >= 12) && (d_d <= 30)) | |
{ | |
m_m = m_m + 3; | |
d_d = d_d - 11; | |
} | |
else if ((m_m == 12) && (d_d >= 1) && (d_d <= 9)) | |
{ | |
m_m = m_m + 2; | |
d_d = d_d + 19; | |
} | |
else if ((m_m == 12) && (d_d >= 10) && (d_d <= 30)) | |
{ | |
m_m = m_m + 3; | |
d_d = d_d - 9; | |
} | |
} | |
if (m_m > 12) | |
{ | |
y_y = y_y + 1; | |
m_m = m_m - 12; | |
} | |
DateTime ds = new DateTime(y_y, m_m, d_d); | |
Dow = (ushort)(DayOfWeek_To_Int(ds.DayOfWeek) % 7); | |
d_d = d_d + e_e - Dow; | |
if (d_d == 0) | |
{ | |
m_m--; | |
switch (m_m) | |
{ | |
case 1: | |
case 3: | |
case 5: | |
case 7: | |
case 8: | |
case 10: | |
case 12: d_d = 31; break; | |
case 2: d_d = 28; break; | |
case 4: | |
case 6: | |
case 9: | |
case 11: d_d = 30; break; | |
default: d_d = 0; break; | |
} | |
} | |
if (m_m == 0) | |
{ | |
y_y--; | |
m_m = 12; | |
} | |
Year = (ushort)y_y; | |
Month = (ushort)m_m; | |
Day = (ushort)d_d; | |
} | |
//****************************************************************************** | |
public static int Get_Sub_Date(int cod) | |
{ | |
DateTime ps1; | |
int Year, Month, Day, Dow, Hour, Min, Sec, MSec; | |
ps1 = DateTime.Now; | |
Year = ps1.Year; | |
Month = ps1.Month; | |
Day = ps1.Day; | |
Hour = ps1.Hour; | |
Min = ps1.Minute; | |
Sec = ps1.Second; | |
MSec = ps1.Millisecond; | |
Dow = (int)ps1.DayOfWeek; | |
CDate.Get_Date_Shams(ref Year, ref Month, ref Day, ref Dow); | |
switch (cod) | |
{ | |
case 0: return Year; | |
case 1: return Month; | |
case 2: return Day; | |
case 3: return Dow; | |
case 4: | |
{ | |
if (Year > 1400) return Year - 1400; | |
else if (Year > 1300) return Year - 1300; | |
else return Year; | |
} | |
case 5: return Hour; | |
case 6: return Min; | |
case 7: return Sec; | |
case 8: return MSec; | |
case 9: return Year * 10000 + Month * 100 + Day; | |
case 10: return Hour * 10000 + Min * 100 + Sec; | |
default: return Hour * 10000 + Min * 100 + Sec; | |
} | |
} | |
public static void DecodeDate_Date(DateTime dx, ref int Year, ref int Month, ref int Day) | |
{ | |
Year = (int)dx.Year; | |
Month = (int)dx.Month; | |
Day = (int)dx.Day; | |
} | |
public static void DecodeDate_Time(DateTime dx, ref int Hour, ref int Min, ref int Sec, ref int MSec) | |
{ | |
Hour = (int)dx.Hour; | |
Min = (int)dx.Minute; | |
Sec = (int)dx.Second; | |
MSec = (int)dx.Millisecond; | |
} | |
public static DateTime EncodeDate(int Year, int Month, int Day) | |
{ | |
DateTime ps = new DateTime((int)Year, (int)Month, (int)Day); | |
return ps; | |
} | |
public static string Get_Month_Eng(byte num) | |
{ | |
switch (num) | |
{ | |
case 1: return "January"; | |
case 2: return "February"; | |
case 3: return "March"; | |
case 4: return "April"; | |
case 5: return "May"; | |
case 6: return "June"; | |
case 7: return "July"; | |
case 8: return "August"; | |
case 9: return "September"; | |
case 10: return "October"; | |
case 11: return "November"; | |
case 12: return "December"; | |
default: return ""; | |
} | |
} | |
public static string Get_Month_Far(int num) | |
{ | |
switch (num) | |
{ | |
case 1: return "فروردين"; | |
case 2: return "ارديبهشت"; | |
case 3: return "خرداد"; | |
case 4: return "تير"; | |
case 5: return "مرداد"; | |
case 6: return "شهريور"; | |
case 7: return "مهر"; | |
case 8: return "آبان"; | |
case 9: return "آذر"; | |
case 10: return "دي"; | |
case 11: return "بهمن"; | |
case 12: return "اسفند"; | |
default: return ""; | |
} | |
} | |
public static string Get_Dow_Far(int num)//rahim 123 | |
{ | |
switch (num) | |
{ | |
case 6: return "شنبه"; | |
case 0: return "يكشنبه"; | |
case 1: return "دوشنبه"; | |
case 2: return "سه شنبه"; | |
case 3: return "چهارشنبه"; | |
case 4: return "پنج شنبه"; | |
case 5: return "جمعه"; | |
default: return ""; | |
} | |
} | |
public static string Get_Day(int RozBaSallMabna) | |
{ | |
string str = ""; | |
switch (RozBaSallMabna % 7) | |
{ | |
case 0: str = "شنبه"; break; | |
case 1: str = "يکشنبه"; break; | |
case 2: str = "دوشنبه"; break; | |
case 3: str = "سه شنبه"; break; | |
case 4: str = "چهارشنبه"; break; | |
case 5: str = "پنج شنبه"; break; | |
case 6: str = "جمعه"; break; | |
} | |
return str; | |
} | |
public static string Get_Dow_Eng(byte num) | |
{ | |
switch (num) | |
{ | |
case 1: return "Saturday"; | |
case 2: return "Sunday"; | |
case 3: return "Monday"; | |
case 4: return "Tuesday"; | |
case 5: return "wednesday"; | |
case 6: return "Thursday"; | |
case 7: return "Friday"; | |
default: return ""; | |
} | |
} | |
public static string Get_Dow_Far(DayOfWeek num)//Rahim 123 | |
{ | |
switch ((byte)num) | |
{ | |
case 6: return "شنبه"; | |
case 7: return "يكشنبه"; | |
case 0: return "يكشنبه"; | |
case 1: return "دوشنبه"; | |
case 2: return "سه شنبه"; | |
case 3: return "چهارشنبه"; | |
case 4: return "پنج شنبه"; | |
case 5: return "جمعه"; | |
default: return ""; | |
} | |
} | |
public static int month(int date) | |
{ | |
ushort y; | |
y = (ushort)(date / 10000); | |
return (date - y * 10000) / 100; | |
} | |
public static int year(int date) | |
{ | |
return date / 10000; | |
} | |
public static int day(int date) | |
{ | |
int y, m; | |
int k; | |
y = date / 10000; | |
k = date - y * 10000; | |
m = k / 100; | |
return (k - m * 100); | |
} | |
public static int DayOfWeek_To_Int(DayOfWeek tar) | |
{ | |
switch (tar) | |
{ | |
case DayOfWeek.Sunday: return 1; | |
case DayOfWeek.Monday: return 2; | |
case DayOfWeek.Tuesday: return 3; | |
case DayOfWeek.Wednesday: return 4; | |
case DayOfWeek.Thursday: return 5; | |
case DayOfWeek.Friday: return 6; | |
case DayOfWeek.Saturday: return 7; | |
default: return 0; | |
} | |
} | |
public static int BefDate(int rooz) | |
{ | |
int Year = 0, Month = 0, Day = 0, Dow = 0; | |
DateTime ps1; | |
if ((rooz < 0) || (rooz > 1000)) | |
{ | |
rooz = 30; | |
} | |
ps1 = DateTime.Now; | |
DecodeDate_Date(ps1, ref Year, ref Month, ref Day); | |
Get_Date_Shams(ref Year, ref Month, ref Day, ref Dow); | |
int d_y = (rooz / 365); | |
Year -= d_y; | |
rooz = (rooz % 365); | |
int d_m = (rooz / 30); | |
Month -= d_m; | |
if (Month < 1) | |
{ | |
Month += 12; | |
Year--; | |
} | |
rooz = (rooz % 30); | |
int d_r = rooz; | |
Day -= d_r; | |
if (Day < 1) | |
{ | |
Month--; | |
if (Month < 7) Day += 31; | |
else Day += 30; | |
} | |
if (Month < 1) | |
{ | |
Month += 12; | |
Year--; | |
} | |
return ((int)Year * 10000 + (int)Month * 100 + (int)Day); | |
} | |
public static int[] Part_Date() | |
{ | |
int[] ret = new int[4]; | |
DateTime ps1; | |
ps1 = DateTime.Now; | |
int FYear = ps1.Year; | |
int FMonth = ps1.Month; | |
int FDay = ps1.Day; | |
int FDow = (int)(ps1.DayOfWeek); | |
Get_Date_Shams(ref FYear, ref FMonth, ref FDay, ref FDow); | |
ret[0] = FYear; | |
ret[1] = FMonth; | |
ret[2] = FDay; | |
ret[3] = FDow; | |
return ret; | |
} | |
public static int ToDay() | |
{ | |
DateTime ps1; | |
ps1 = DateTime.Now; | |
int FYear = ps1.Year; | |
int FMonth = ps1.Month; | |
int FDay = ps1.Day; | |
int FDow = (int)(ps1.DayOfWeek); | |
Get_Date_Shams(ref FYear, ref FMonth, ref FDay, ref FDow); | |
//int FYear2 = FYear - 1300; | |
return FYear * 10000 + FMonth * 100 + FDay; | |
} | |
public static int TwoDigitsYear() | |
{ | |
DateTime ps1; | |
ps1 = DateTime.Now; | |
int FYear = ps1.Year; | |
int FMonth = ps1.Month; | |
int FDay = ps1.Day; | |
int FDow = (int)(ps1.DayOfWeek); | |
Get_Date_Shams(ref FYear, ref FMonth, ref FDay, ref FDow); | |
if (FYear >= 1400) return FYear - 1400; | |
else if (FYear >= 1300) return FYear - 1300; | |
else return FYear; | |
} | |
public static int GetTime() | |
{ | |
DateTime d1 = DateTime.Now; | |
return d1.Hour * 10000 + d1.Minute * 100 + d1.Second; | |
} | |
public static bool IsKabise(int Sal) | |
{ | |
int i; | |
if (Sal >= 100) | |
Sal = ((Sal / 100) - 13) * 100 + (Sal % 100); | |
i = ((Sal - 55) % 33); | |
if (i < 0) i += 33; | |
if (i != 32) i = i % 4; | |
if (i == 0) return true; | |
return false; | |
} | |
public static int get_cur_year2() | |
{ | |
return CDate.get_year2(CDate.ToDay()); | |
} | |
public static int get_year2(string date) | |
{ | |
return CDate.get_year2(int.Parse((date).Replace("/", "").Replace(((char)(58)).ToString(), "")));//Replace(((char)(0x0338)).ToString(), ""). | |
} | |
public static int get_year2(int date) | |
{ | |
if ((date % 10000) == 101) return ((date / 10000) % 100) - 1; | |
else return ((date / 10000) % 100); | |
} | |
public static int get_year2_(string date) | |
{ | |
return CDate.get_year2_(int.Parse(((date).Replace("/", "").Replace(((char)(58)).ToString(), ""))));//Replace(((char)(0x0338)).ToString(), "")). | |
} | |
public static int get_year2_(int date) | |
{ | |
return ((date / 10000) % 100); | |
} | |
public static void Get_Date_Shams(ref int Year, ref int Month, ref int Day, ref int Dow) | |
{ | |
int y_y, m_m, d_d, k_k, x_x, e_e, l_l, w_w; | |
y_y = Year; | |
if ((y_y < 100) && (y_y > 50)) y_y = y_y + 1900; | |
else if (y_y < 50) y_y = y_y + 2000; | |
k_k = 1074; | |
x_x = 1; | |
m_m = Month; | |
d_d = Day; | |
e_e = (int)(EncodeDate(y_y, m_m, d_d).DayOfWeek) + 1; | |
if (e_e == 7) e_e = 0; | |
y_y = y_y - 622; | |
if ((m_m > 3) || ((m_m == 3) && (d_d > 20))) | |
{ | |
y_y = y_y + 1; | |
} | |
if ((m_m == 1) && (d_d >= 1) && (d_d <= 20)) | |
{ | |
m_m = m_m + 9; | |
d_d = d_d + 10; | |
} | |
else if ((m_m == 1) && (d_d >= 21) && (d_d <= 31)) | |
{ | |
m_m = m_m + 10; | |
d_d = d_d - 20; | |
} | |
else if ((m_m == 2) && (d_d >= 1) && (d_d <= 19)) | |
{ | |
m_m = m_m + 9; | |
d_d = d_d + 11; | |
} | |
else if ((m_m == 2) && (d_d >= 20) && (d_d <= 29)) | |
{ | |
m_m = m_m + 10; | |
d_d = d_d - 19; | |
} | |
else if ((m_m == 3) && (d_d >= 1) && (d_d <= 20)) | |
{ | |
m_m = m_m + 9; | |
d_d = d_d + 9; | |
} | |
else if ((m_m == 3) && (d_d >= 21) && (d_d <= 31)) | |
{ | |
m_m = m_m - 2; | |
d_d = d_d - 20; | |
} | |
else if ((m_m == 4) && (d_d >= 1) && (d_d <= 20)) | |
{ | |
m_m = m_m - 3; | |
d_d = d_d + 11; | |
} | |
else if ((m_m == 4) && (d_d >= 21) && (d_d <= 30)) | |
{ | |
m_m = m_m - 2; | |
d_d = d_d - 20; | |
} | |
else if ((m_m == 5) && (d_d >= 1) && (d_d <= 21)) | |
{ | |
m_m = m_m - 3; | |
d_d = d_d + 10; | |
} | |
else if ((m_m == 5) && (d_d >= 22) && (d_d <= 31)) | |
{ | |
m_m = m_m - 2; | |
d_d = d_d - 21; | |
} | |
else if ((m_m == 6) && (d_d >= 1) && (d_d <= 21)) | |
{ | |
m_m = m_m - 3; | |
d_d = d_d + 10; | |
} | |
else if ((m_m == 6) && (d_d >= 22) && (d_d <= 30)) | |
{ | |
m_m = m_m - 2; | |
d_d = d_d - 21; | |
} | |
else if ((m_m == 7) && (d_d >= 1) && (d_d <= 22)) | |
{ | |
m_m = m_m - 3; | |
d_d = d_d + 9; | |
} | |
else if ((m_m == 7) && (d_d >= 23) && (d_d <= 31)) | |
{ | |
m_m = m_m - 2; | |
d_d = d_d - 22; | |
} | |
else if ((m_m == 8) && (d_d >= 1) && (d_d <= 22)) | |
{ | |
m_m = m_m - 3; | |
d_d = d_d + 9; | |
} | |
else if ((m_m == 8) && (d_d >= 23) && (d_d <= 31)) | |
{ | |
m_m = m_m - 2; | |
d_d = d_d - 22; | |
} | |
else if ((m_m == 9) && (d_d >= 1) && (d_d <= 22)) | |
{ | |
m_m = m_m - 3; | |
d_d = d_d + 9; | |
} | |
else if ((m_m == 9) && (d_d >= 23) && (d_d <= 30)) | |
{ | |
m_m = m_m - 2; | |
d_d = d_d - 22; | |
} | |
else if ((m_m == 10) && (d_d >= 1) && (d_d <= 22)) | |
{ | |
m_m = m_m - 3; | |
d_d = d_d + 8; | |
} | |
else if ((m_m == 10) && (d_d >= 23) && (d_d <= 31)) | |
{ | |
m_m = m_m - 2; | |
d_d = d_d - 22; | |
} | |
else if ((m_m == 11) && (d_d >= 1) && (d_d <= 21)) | |
{ | |
m_m = m_m - 3; | |
d_d = d_d + 9; | |
} | |
else if ((m_m == 11) && (d_d >= 22) && (d_d <= 30)) | |
{ | |
m_m = m_m - 2; | |
d_d = d_d - 21; | |
} | |
else if ((m_m == 12) && (d_d >= 1) && (d_d <= 21)) | |
{ | |
m_m = m_m - 3; | |
d_d = d_d + 9; | |
} | |
else if ((m_m == 12) && (d_d >= 22) && (d_d <= 31)) | |
{ | |
m_m = m_m - 2; | |
d_d = d_d - 21; | |
} | |
w_w = (int)((365 * (y_y - k_k) + (int)((8 * (y_y - k_k)) / 33) + 31 * (m_m - 1) - (int)(m_m / 7) * (m_m % 7) + d_d + x_x) % 7); | |
l_l = (int)((((8 * (y_y - k_k + 1)) / 33)) - (int)(((8 * (y_y - k_k)) / 33))); | |
if ((w_w < e_e) || ((w_w == 6) && (e_e == 0))) | |
{ | |
d_d = d_d + 1; | |
w_w = e_e; | |
if (((m_m >= 1) && (m_m <= 6) && (d_d > 31)) || ((m_m >= 7) && (m_m <= 11) && (d_d > 30))) | |
{ | |
d_d = 1; | |
m_m = m_m + 1; | |
} | |
else if ((m_m == 12) && (((d_d > 30) && (l_l == 1)) || ((d_d > 29) && (l_l == 0)))) | |
{ | |
d_d = 1; | |
m_m = 1; | |
y_y = y_y + 1; | |
} | |
} | |
if ((w_w > e_e) || ((w_w == 0) && (e_e == 6))) | |
{ | |
d_d = d_d - 1; | |
if ((m_m >= 2) && (m_m <= 7) && (d_d == 0)) | |
{ | |
d_d = 31; | |
m_m = m_m - 1; | |
if ((m_m >= 8) && (m_m <= 12) && (d_d == 0)) | |
{ | |
d_d = 30; | |
m_m = m_m - 1; | |
} | |
else if ((m_m == 1) && (d_d == 0)) | |
{ | |
y_y = y_y - 1; | |
l_l = (int)((((8 * (y_y - k_k + 1)) / 33)) - (int)(((8 * (y_y - k_k)) / 33))); | |
if (l_l == 0) d_d = 29; | |
else d_d = 30; | |
m_m = 12; | |
} | |
} | |
} | |
y_y = y_y - 1120; | |
int cons1 = 278, cons2 = 1352; | |
//string SAKO = TAPP1.MARKAZ; | |
// if (((SAKO == "FNS") || (SAKO == "FSN") || (SAKO == "YAZ") || (SAKO == "BOS") || (SAKO == "CHR") || (SAKO == "KHN") || (SAKO == "KOR") || (SAKO == "HSM") || (SAKO == "ARD") || (SAKO == "AZG") || (SAKO == "ESF") || (SAKO == "SYSA") || (SAKO == "AZR") || (SAKO == "ZAN") || (SAKO == "KSH") || (SAKO == "HAM") || (SAKO == "SYS") || (SAKO == "FRS") || (SAKO == "KRM")) && ((m_m < 6) || (m_m == 6) && (d_d <= 31))) //6_MONTH | |
//if (((SAKO == "FNS") || (SAKO == "FSN") || (SAKO == "YAZ") || (SAKO == "BOS") || (SAKO == "CHR") || (SAKO == "KHN") || (SAKO == "KOR") || (SAKO == "ARD") || (SAKO == "AZG") || (SAKO == "ESF") || (SAKO == "SYSA") || (SAKO == "AZR") || (SAKO == "ZAN") || (SAKO == "KSH") || (SAKO == "HAM") || (SAKO == "SYS") || (SAKO == "FRS") || (SAKO == "FRSA") || (SAKO == "KRM") || (SAKO == "KOH") || (SAKO == "LOR")) && ((m_m < 7) || (m_m == 7) && (d_d <= 31))) //7_MONTH | |
//{ | |
// cons1 = 277; | |
// cons2 = 1351; | |
//} | |
//else if (((SAKO == "KHS") || (SAKO == "KHR")) && ((m_m < 4) || (m_m == 4) && (d_d <= 31))) | |
//{ | |
// cons1 = 277; | |
// cons2 = 1351; | |
//} | |
//else if ((SAKO == "ALI") && ((m_m < 2) || (m_m == 2) && (d_d <= 31))) | |
//{ | |
// cons1 = 277; | |
// cons2 = 1351; | |
//} | |
//else if ((SAKO == "HSM") || (SAKO == "HEMA") || (SAKO == "HEMB") || (SAKO == "HEMD") || (SAKO == "HEME") || (SAKO == "HEMC") || (SAKO == "HEMF") || (SAKO == "HEMG") || (SAKO == "HEMH") || (SAKO == "HEMI") || (SAKO == "HEMJ") || (SAKO == "AZRI") || (SAKO == "AZRJ") || (SAKO == "AZRK") || (SAKO == "AZRH") || (SAKO == "AZRM") || (SAKO == "MAZI") || (SAKO == "NOB"))//1_YEAR | |
//{ | |
// cons1 = 277; | |
// cons2 = 1351; | |
//} | |
//else | |
//{ | |
// cons1 = 276; | |
// cons2 = 1350; | |
//} | |
if (y_y > cons1)///Sall_Kari_Change | |
{ | |
y_y = y_y - 1075; | |
m_m -= 3; | |
d_d -= 2; | |
} | |
y_y = y_y + 1074; | |
if (y_y > cons2)///Sall_Kari_Change | |
{ | |
y_y = y_y - 1192; | |
m_m++; | |
d_d--; | |
} | |
y_y = y_y + 46; | |
Year = y_y; | |
Month = m_m; | |
Day = d_d; | |
} | |
public static int Get_Difference_shamsi(int FirstDate, int LastDate) | |
{ | |
int i, yy1, mm1, dd1, yy2, mm2, dd2, sum1, sum2, zarib; | |
if (FirstDate > LastDate) | |
{ | |
i = FirstDate; | |
FirstDate = LastDate; | |
LastDate = i; | |
zarib = -1; | |
} | |
else zarib = 1; | |
dd1 = FirstDate % 100; | |
dd2 = LastDate % 100; | |
mm1 = (FirstDate / 100) % 100; | |
mm2 = (LastDate / 100) % 100; | |
yy1 = FirstDate / 10000; | |
yy2 = LastDate / 10000; | |
sum1 = 0; | |
for (i = mm1 - 1; i >= 1; i--) | |
{ | |
if (i > 6) sum1 = sum1 + 30; | |
else sum1 = sum1 + 31; | |
} | |
sum1 = sum1 + dd1; | |
sum2 = 0; | |
for (i = mm2 - 1; i >= 1; i--) | |
{ | |
if (i > 6) sum2 = sum2 + 30; | |
else sum2 = sum2 + 31; | |
} | |
sum2 = sum2 + dd2; | |
for (i = yy2 - 1; i >= yy1; i--) | |
{ | |
if (IsKabise(i)) sum2 = sum2 + 366; | |
else sum2 = sum2 + 365; | |
} | |
return (sum2 - sum1) * zarib; | |
} | |
public static int Get_Roz_Sall(int date) | |
{ | |
return (date % 100) + (((((date % 10000) / 100) - 1) * 31) - ((((date % 10000) / 100) / 8) * (((date % 10000) / 100) - 7))); | |
} | |
public static int Get_Roz_Hafteh_I(int date) | |
{ | |
int Sall, RozBaSallMabna; | |
int Sa = 365; | |
Sall = date / 10000; | |
Sall = Sall - 1375; | |
//if ((Sall%4)==0) Sa = 366; | |
RozBaSallMabna = Sall * Sa + Sall / 4 + Get_Roz_Sall(date) + 4; | |
return RozBaSallMabna % 7; | |
} | |
public static string Get_Roz_Hafteh_S2(int date) | |
{ | |
int Sall, RozBaSallMabna; | |
int Sa = 365; | |
Sall = date / 10000; | |
Sall = Sall - 1375; | |
//if ((Sall%4)==0) Sa = 366; | |
RozBaSallMabna = Sall * Sa + Sall / 4 + Get_Roz_Sall(date) + 4; | |
string str = ""; | |
switch (RozBaSallMabna % 7) | |
{ | |
case 0: str = "شنبه"; break; | |
case 1: str = "يکشنبه"; break; | |
case 2: str = "دوشنبه"; break; | |
case 3: str = "سه شنبه"; break; | |
case 4: str = "چهارشنبه"; break; | |
case 5: str = "پنج شنبه"; break; | |
case 6: str = "جمعه"; break; | |
} | |
return str; | |
} | |
public static string Get_Roz_Hafteh_S(int date) | |
{ | |
int[] TI = CDate.Part_Date(); | |
return Get_Dow_Far(TI[3]); | |
} | |
public static string cur_time() | |
{ | |
return DateTime.Now.TimeOfDay.ToString().Substring(0, 8); | |
} | |
public static string cmon(int m) | |
{ | |
switch (m) | |
{ | |
case 1: return "فروردين"; | |
case 2: return "ارديبهشت"; | |
case 3: return "خرداد"; | |
case 4: return "تير"; | |
case 5: return "مرداد"; | |
case 6: return "شهريور"; | |
case 7: return "مهر"; | |
case 8: return "آبان"; | |
case 9: return "آذر"; | |
case 10: return "دي"; | |
case 11: return "بهمن"; | |
case 12: return "اسفند"; | |
default: return ""; | |
} | |
} | |
public static string cdow(string date) | |
{ | |
switch (DateTime.Now.DayOfWeek) | |
{ | |
case DayOfWeek.Sunday: return "يكشنبه"; | |
case DayOfWeek.Monday: return "دوشنبه"; | |
case DayOfWeek.Tuesday: return "سه شنبه"; | |
case DayOfWeek.Wednesday: return "چهارشنبه"; | |
case DayOfWeek.Thursday: return "پنجشنبه"; | |
case DayOfWeek.Friday: return "جمعه"; | |
case DayOfWeek.Saturday: return "شنبه"; | |
default: return ""; | |
} | |
} | |
public static string dates() | |
{ | |
return DateTime.Now.ToShortDateString(); | |
} | |
public static int get_date_F() | |
{ | |
return get_date_F(0); | |
} | |
public static int get_date_F(int dur_roz) | |
{ | |
int y_y, m_m, d_d, e_e; | |
DateTime ps1 = DateTime.Now; | |
if (dur_roz != 0) | |
{ | |
ps1 = ps1.AddDays(dur_roz); | |
} | |
y_y = ps1.Year; | |
m_m = ps1.Month; | |
d_d = ps1.Day; | |
e_e = CDate.DayOfWeek_To_Int(ps1.DayOfWeek); | |
CDate.Get_Date_Shams(ref y_y, ref m_m, ref d_d, ref e_e); | |
return (y_y * 10000 + m_m * 100 + d_d); | |
} | |
public static int get_date_E() | |
{ | |
ushort Year, Month, Day; | |
DateTime ps1 = DateTime.Now; | |
Year = (ushort)ps1.Year; | |
Month = (ushort)ps1.Month; | |
Day = (ushort)ps1.Day; | |
return Year * 10000 + Month * 100 + Day; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment