0

I have a start date like "2015-03-10". I want to add 1.25 days per month for the current year from this start date. For example, I have start date "2015-03-10" then for this year the number of days will be 12.50. (1.25 days for each month from March month).

Given date : 2015-11-10 need to add days up to March 31st,2016 : 1.25 * 5 (as from November month, here 5 )(For every month add 1.25 days) Number of days for this year : 6.25 (year start calculated from April and year end considered as March.)

How can i do this in c# can any one help me to do this Thanks in advance

KaviSuja
  • 450
  • 1
  • 9
  • 37
  • 1
    How come the SQL tag? – jarlh Dec 15 '15 at 13:02
  • @jarlh sorry yes its not there thank you – KaviSuja Dec 15 '15 at 13:03
  • 2
    It's very unclear what you're asking for here - there seem to be two different scenarios - one where the end of the year is December, and one where it's March. Please try to put together a [mcve]. Also note that from 2015-03-10 to March 2016 is 12 months, not 10... – Jon Skeet Dec 15 '15 at 13:03
  • Possible duplicate of this http://stackoverflow.com/questions/1525990/calculating-the-difference-in-months-between-two-dates –  Dec 15 '15 at 13:06
  • Have you tried to implement it? .NET Framework has a great [`DateTime`](https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx) structure. – Sergii Zhevzhyk Dec 15 '15 at 13:11
  • What if for months before March like January and February? It should be end of the year or it should be the next March? – Soner Gönül Dec 15 '15 at 13:30
  • @SonerGönül if january then 1.25 * 2 = 2.50 (ianuary should consider as 10th month of the year here) – KaviSuja Dec 15 '15 at 13:32

2 Answers2

1

This example ignores the days of the months, as per your post:

DateTime start;
DateTime end;

// assuming end > start
double value = 1.25 * (end.Month - start.Month + 12 * (end.Year - start.Year) + 1);

EDIT: I can help you to understand this line of code splitting in some steps:

DateTime start = DateTime.Now;
DateTime end = new DateTime(start.Year, 3, 31);

if (start.Month > 3) 
    end = end.AddYear(1);

double years = end.Year - start.Year;
double months = end.Month - start.Month + 1;
months += (years * 12);
double value = 1.25 * months;
  • thanks for ur reply. I have start date as current date. How can i get end date as financial year (March 31 st of next year) – KaviSuja Dec 15 '15 at 13:49
  • Sorry I am not sure to get your question. You want to autocalculate the end date in some way? Is it always the next March 31? If it is then "if (start.Month <= 3) end = new DateTime(start.Year, 3, 31); else end = new DateTime(start.Year + 1, 3, 31);" –  Dec 15 '15 at 13:52
  • i get the last date of next year march 31st by following : DateTime endDate = new DateTime(DateTime.Today.Year + 1, 4, 1).AddDays(-1); and now want to get 1.25 * (diff between the months). Here diff between months should be 5 (thats november month should taken as 8 th month of financial year)... – KaviSuja Dec 15 '15 at 14:17
  • Why "new DateTime(DateTime.Today.Year + 1, 4, 1).AddDays(-1)" and not "new DateTime(DateTime.Today.Year + 1, 3, 31)". Anyway this doesn't work for January and February (see my previous comment). –  Dec 15 '15 at 14:54
0

I got it by following :

DateTime endDate = new DateTime(DateTime.Today.Year + 1, 4, 1).AddDays(-1);
if (Convert.ToDateTime(empHiredDate).Month > 4)
{
    finMonth = Convert.ToDateTime(empHiredDate).Month - 4;
    finMonth = 12 - finMonth;
    avail =Convert.ToString(finMonth * 1.25);
}
else if (Convert.ToDateTime(empHiredDate).Month < 3)
{
    finMonth = Convert.ToDateTime(empHiredDate).Month + 8;
    finMonth = 12 - finMonth;
    avail = Convert.ToString(finMonth * 1.25);
}
else if (Convert.ToDateTime(empHiredDate).Month == 4)
{
    avail = Convert.ToString(12 * 1.25);
}
else if (Convert.ToDateTime(empHiredDate).Month == 3)
{
    avail = Convert.ToString(1 * 1.25);
}
cramopy
  • 3,459
  • 6
  • 28
  • 42
KaviSuja
  • 450
  • 1
  • 9
  • 37
  • This is not what you asked. For today date (2015 Dicember, 16) you will output 9*1.25=11.25. Your question was unclear and so is your solution. –  Dec 16 '15 at 08:15
  • Now it seems to works, but it is unnecessarily complicated. You have written about 10 lines of code and you get same results as my answer (1 single line of code). My feeling is that you're trying to follow some logical steps to get the results, but you have to learn how to reduce "complex" problems to simple operations. –  Dec 16 '15 at 13:28
  • I have edited my answer to help you to understand it. –  Dec 16 '15 at 13:33