I have two days field, from that ..need to calculate num of days,month and year.. like if two dates are as
12:02:2008 & 15:03:2008
the result should be .
days: 1 days
month: 1 month( consider 31 days / month)
year: 0 years
plz guide me
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Blocked AccountRank #403 · 6.2K reputationPosted Nov 7, 2008, 1:12 AM EST
Hi,
You can perform that task by using this code.
DateTime gTime = DateTime.Parse("12/10/2008");
DateTime lTime = DateTime.Parse("11/7/2008");
string day = (gTime.Day - lTime.Day).ToString();
string month = (gTime.Month - lTime.Month).ToString();
string year = (gTime.Year - lTime.Year).ToString();
Happy Coding!!
shashank kale8 reputationPosted Nov 7, 2008, 8:18 AM EST
then
datetime date1=(datetime)datetimepicker1.value;// first date
datetime date2=(datetime)datetimepicker2.value;//second date
timespan ts=date2.substract(date1);
textbox1.text=ts.days;
u will got days
i thing this is ur answere....
Satish KathiRank #8,369 · 223 reputationPosted Nov 7, 2008, 9:26 AM EST
DateTime gTime = Convert.ToDateTime(maskedTextBox2.Text);
DateTime lTime = Convert.ToDateTime(maskedTextBox1.Text);
int year = 0;
int month = 0;
int day = 0;
year = gTime.Year - lTime.Year;
if (gTime.Month > lTime.Month)
month = gTime.Month - lTime.Month;
else if (gTime.Month == lTime.Month)
{
month = 0;
}
else
{
month = gTime.Month - lTime.Month + 12;
year--;
}
if (gTime.Day > lTime.Day)
day = gTime.Day - lTime.Day;
else if (gTime.Day == lTime.Day)
{
day = 0;
}
else
{
DateTime ndt = (new DateTime(gTime.Year, gTime.Month, 1)).AddDays(-1);
day = ndt.Day - (lTime.Day - gTime.Day);
month--;
}
if (month < 0)
{
month = month + 12;
year = year - 1;
}
if (year < 0)
{
year = year + 1;
}
label1.Text = string.Format("{0} Years - {1} Months - {2} Days", year, month, day);
Ryan AlfordRank #1,411 · 1.2K reputationPosted Nov 8, 2008, 5:46 AM EST
AlanRank #669 · 3.6K reputationPosted Nov 8, 2008, 9:30 AM EST
Everyone seems to have overlooked that santosh asked for the date difference to be calculated as if a month were always 31 days and (presumably therefore) a year were 31 * 12 = 372 days:
DateTime start = DateTime.Parse("12/02/2008");
DateTime end = DateTime.Parse("15/03/2008");
int days = (end - start).Days;
int months = days / 31;
days = days % 31;
int years = months / 12;
months = months % 12;
string result = String.Format
(
"Days : {0}\r\nMonths : {1}\r\nYears : {2}", days, months, years
);
MessageBox.Show(result);