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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Nov 8, 2008, 2:30 PM
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);
Ryan AlfordPosted Nov 8, 2008, 10:46 AM
Satish KathiPosted Nov 7, 2008, 2:26 PM
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);
shashank kalePosted Nov 7, 2008, 1:18 PM
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....
Blocked AccountPosted Nov 7, 2008, 6:12 AM
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!!