PDA

View Full Version : سوال: نشان دادن اختلاف دو تاریخ شمسی به صورت سال، ماه، روز



salehsam
شنبه 07 دی 1392, 17:54 عصر
سلام دوستان
من با کد زیر اختلاف دو تاریخ رو بدست میارم


System.Globalization.PersianCalendar calendar = new System.Globalization.PersianCalendar();
DateTime dt1 = calendar.ToDateTime(1390, 5, 15, 0, 0, 0, 0);
DateTime dt2 = calendar.ToDateTime(1390, 9, 19, 0, 0, 0, 0);

TimeSpan ts = dt2.Subtract(dt1);
int days = ts.Days;



با این کد تعداد روز ها رو نشون میده حالا چیکار کنم که سال، ماه و روز رو برگردونه!
ممنون

matin.soft
شنبه 07 دی 1392, 18:09 عصر
http://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days

salehsam
شنبه 07 دی 1392, 18:34 عصر
ممنون دوست من
ولی من میخوام بهم بگه که چند سال و چند ماه و چند روز فرق دارن نه چند روز!
چیکار کنم؟

salehsam
شنبه 07 دی 1392, 22:52 عصر
من با این کد تونستم سال رو دقیق بدست بیارم ولی ماه و روز و درست نشون نمیده!


string[] date1 = txt_birthdate.Text.Split('/');

int y1 = int.Parse(date1[0]);
int m1 = int.Parse(date1[1]);
int d1 = int.Parse(date1[2]);
System.Globalization.PersianCalendar calendar = new System.Globalization.PersianCalendar();
label25.Text = Convert.ToString(calendar.GetYear(DateTime.Now) - y1) + "سال" +
Convert.ToString(calendar.GetMonth(DateTime.Now) - m1) + "ماه" +
Convert.ToString(calendar.GetDayOfMonth(DateTime.N ow) - d1) + "روز";

دوستان نظرتون چیه؟

fmehrvarzi
یک شنبه 08 دی 1392, 08:04 صبح
یه راه سادده تر :لبخند:
int Months=((dt2.Year - dt1.Year) * 12) + (dt2.Month - dt1.Month);
int Years=(dt2.Year - dt1.Year) ;

salehsam
یک شنبه 08 دی 1392, 08:37 صبح
ممنون
این سال و ماه رو برمیگردونه
برای روز چیکار کنم؟

fmehrvarzi
یک شنبه 08 دی 1392, 10:21 صبح
روز رو که خودت نوشته بودی! حواست کجاست؟
TimeSpan ts = dt2.Subtract(dt1);
int days = ts.Days;

salehsam
یک شنبه 08 دی 1392, 14:58 عصر
ممنون دوست من
ولی من کل روزها رو نمیخوام!
یه چیزی شبیه این میخوام :
تاریخ تولد شما : ٢٢/مهر/١٣٧٠

سن شما چقدره؟ : 22 سال 2 ماه 15 روز
این مثال رو سایت http://www.persianfal.com (http://www.persianfal.com/) محاسبه کرده
چیکار کنم که یه چیزی شبیه این بهم بده؟

amir200h
یک شنبه 08 دی 1392, 15:17 عصر
System.Globalization.PersianCalendar persia = new System.Globalization.PersianCalendar();

string FirstDate = "1369/04/28";
string EndDate = "1392/01/14";

Int16 StartYear = Convert.ToInt16(FirstDate.Substring(0, 4));
Int16 StartMonth = Convert.ToInt16(FirstDate.Substring(5, 2));
Int16 StartDay = Convert.ToInt16(FirstDate.Substring(8, 2));

Int16 EndYear = Convert.ToInt16(EndDate.Substring(0, 4));
Int16 EndMonth = Convert.ToInt16(EndDate.Substring(5, 2));
Int16 EndDay = Convert.ToInt16(EndDate.Substring(8, 2));

DateTime StartDateTime = persia.ToDateTime(StartYear, StartMonth, StartDay, 0, 0, 0, 0);
DateTime EndDateTime = persia.ToDateTime(EndYear, EndMonth, EndDay, 0, 0, 0, 0);

TimeSpan Difference = EndDateTime - StartDateTime;


int Days = Difference.Days;
MessageBox.Show(Days.ToString());
int sal, mah, roz;
sal = Days / 365;
MessageBox.Show(sal.ToString());


if (Days % 365 > 186)
{
int Days2 = Days % 365 - 186;
mah = Days2 / 30;
mah += 6;
}
else
{
mah = (Days % 365) / 31;
}


MessageBox.Show(mah.ToString());

if (Days % 365 > 186)
{
int Days3 = Days % 365 - 186;
roz = ((Days3 % 365) % 30);
}
else
{
roz = ((Days % 365) % 31);
}


MessageBox.Show(sal + "/" + mah + "/" + roz);

salehsam
یک شنبه 08 دی 1392, 15:30 عصر
ممنون دوست من ولی :ناراحت: دقیق نیست!
چند روز بیشتر نشون میده!

amir200h
یک شنبه 08 دی 1392, 15:47 عصر
ممنون دوست من ولی :ناراحت: دقیق نیست!
چند روز بیشتر نشون میده!

System.Globalization.PersianCalendar persia = new System.Globalization.PersianCalendar();

string FirstDate = "1370/07/22";
string EndDate = "1392/10/08";

Int16 StartYear = Convert.ToInt16(FirstDate.Substring(0, 4));
Int16 StartMonth = Convert.ToInt16(FirstDate.Substring(5, 2));
Int16 StartDay = Convert.ToInt16(FirstDate.Substring(8, 2));

Int16 EndYear = Convert.ToInt16(EndDate.Substring(0, 4));
Int16 EndMonth = Convert.ToInt16(EndDate.Substring(5, 2));
Int16 EndDay = Convert.ToInt16(EndDate.Substring(8, 2));

DateTime StartDateTime = persia.ToDateTime(StartYear, StartMonth, StartDay, 0, 0, 0, 0);
DateTime EndDateTime = persia.ToDateTime(EndYear, EndMonth, EndDay, 0, 0, 0, 0);

TimeSpan Difference = EndDateTime - StartDateTime;


int Days = Difference.Days;
MessageBox.Show(Days.ToString());
int sal, mah, roz;
sal = Days / 365;
MessageBox.Show(sal.ToString());


if (Days % 365 > 186)
{
int Days2 = Days % 365 - 186;
mah = Days2 / 30;
mah += 6;
}
else
{
mah = (Days % 365) / 31;
}


MessageBox.Show(mah.ToString());

if (Days % 365 > 186)
{
int Days3 = Days % 365 - 186;
roz = ((Days3 % 365) % 30);
}
else
{
roz = ((Days % 365) % 31);
}


MessageBox.Show(sal + "/" + mah + "/" +(roz-(sal/4)));

salehsam
یک شنبه 08 دی 1392, 17:44 عصر
این یکی دقیقتر شد
ممنون