-9

In a C# windows form based application. I used two dateTime pickers,one for From and another for To.

if a employee selecting his leave from may 30 in dateTime picker1 and selecting june 2 in datetime picker2, that makes 4 days of leave.

My question is how can i findout how many days he took leave in may and how many days he took leave in june?

Update I know this, and stuck further

DateTime start = dateTimePicker1.Value.Date; 
DateTime end = dateTimePicker2.Value.Date; 
int a = int.Parse(Convert.ToInt32(end.Subtract(start).Days).ToString());
Amit
  • 1,821
  • 1
  • 17
  • 30
  • 1
    [We can’t rely on other people to do our work for us. Asking others to do our work for us can also be seen as rude. If we try for ourselves, we may succeed.](http://idownvotedbecau.se/noattempt/) – Liam May 25 '18 at 11:06
  • We will be interested in knowing what you have do so far and where do are you stuck .. code will be better – Amit May 25 '18 at 11:07
  • My 4 second search found [this](https://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days) – Lucax May 25 '18 at 11:08
  • @Lucax he would need this but still he need to find how many days in one month and how many in other ! – Amit May 25 '18 at 11:09
  • Possible duplicate of [Calculate difference between two dates (number of days)?](https://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days) – rawwar May 25 '18 at 11:09
  • actually am a degree student... currently am doing a payroll mangament System.. – Nidheesh K May 25 '18 at 11:09
  • Are you get paid for doing payroll management System? – Fabio May 25 '18 at 11:10
  • @NidheeshK dear if you are student, you must try your homework by yourself. that way you will learn better – Amit May 25 '18 at 11:10
  • Welcome to stack overflow. As others have pointed out, here it is required to first try to solve the problem yourself and then ask a question when you run into a specific problem. The reason is that most ppl answer the questions in their free time while they are getting paid to develop professionally. They are used to the idea that software development is not done for free, while helping others for their specific problems on SO is. – Sefe May 25 '18 at 11:11
  • a got solution for difference between two days – Nidheesh K May 25 '18 at 11:11
  • @NidheeshK will it work for your requirement ? – Amit May 25 '18 at 11:11
  • its our finally year project,,,, and we dont anything in this semester,but we have to submit project,so choose C# platform, but while reach to leave part module i got stucked – Nidheesh K May 25 '18 at 11:13
  • DateTime start = dateTimePicker1.Value.Date; DateTime end = dateTimePicker2.Value.Date; int a = int.Parse(Convert.ToInt32(end.Subtract(start).Days).ToString()); – Nidheesh K May 25 '18 at 11:13
  • difffrence between two days its working – Nidheesh K May 25 '18 at 11:14
  • but the thing is,if choose LEAVE FROM:may 30 in datetime picker1 and LEAVE_TO:june 2 in datetimepicker2, how can i findout how many days he took leave in may as well as june..while doing subtract am getting 4.i want ,result in may he took 2 leaves and in june he took 2 leaves – Nidheesh K May 25 '18 at 11:19
  • please help me..ddnt get solution on youtube.. so am stucked – Nidheesh K May 25 '18 at 11:21
  • am not paying.. we have two options in college either we can ask help of small startup companies or else u should ourself, am doing myself.. – Nidheesh K May 25 '18 at 11:34
  • If you have additional information, please edit that into your question. – Hans Kesting May 25 '18 at 11:39
  • 1
    Review: `end.Subtract(start).Days` already returns an `int` value. So you don't need to `Convert.ToInt()`. And why would to use `.ToString()` on an int value, only to `int.Parse()` it back into an int? – Hans Kesting May 25 '18 at 11:42
  • This question can be anything else but far away from "too broad" – Fabio May 26 '18 at 21:55

3 Answers3

2

you can try this code,

here at the end you will get a Dictionary (tabular form) where key will be month number and value is days of in month (here in your question, how many days employee has taken leave)

DateTime startDate = new DateTime(2018, 06, 25); //date time picker's date
DateTime endDate = new DateTime(2018, 07, 05); //date time picker's date
Dictionary<int, int> daysInMonth = new Dictionary<int, int>();

while(true)
{
    DateTime thisMonthEndDate = new DateTime(startDate.Year, startDate.Month, DateTime.DaysInMonth(startDate.Year, startDate.Month));
    if (thisMonthEndDate > endDate)
    {
        thisMonthEndDate = endDate;
        daysInMonth.Add(startDate.Month, (int)(thisMonthEndDate - startDate).TotalDays + 1);
        break;
    }

    daysInMonth.Add(startDate.Month, (int)(thisMonthEndDate - startDate).TotalDays + 1);
    startDate = thisMonthEndDate.AddDays(1);
}

and output printing will be

foreach(KeyValuePair<int, int> keyVal in daysInMonth)
{
    Console.WriteLine("For Month:" + keyVal.Key + " leave count:" + keyVal.Value);
}

enter image description here

Amit
  • 1,821
  • 1
  • 17
  • 30
1

To get days per month in a timespan.

   for (DateTime date = start; date < end; date = date.AddMonths(1))
   {
       DateTime this_date_start = new DateTime(date.Year, date.Month, 1);
       DateTime this_date_end = this_date_start.AddMonths(1).AddDays(-1);
       TimeSpan duration = this_date_end.Subtract(date);
       Console.WriteLine("duration " + date.Month + " = " + duration.Days  + "days");
   }

NB: this loop is incomplete: then you have to do this once again outside the loop (this will also catch any shorter than one month spans)

Mr Heelis
  • 2,370
  • 4
  • 24
  • 34
1

LINQ make it simple and readable:

var from = new DateTime(2018, 5, 28);
var to = new DateTime(2018, 6, 4);

var result = Enumerable.Range(0, int.MaxValue)
                       .Select(i => from.Date.AddDays(i))
                       .TakeWhile(date => date <= to.Date)
                       .GroupBy(date => date.Month)
                       .Select(range => (Month: range.Key, Days: range.Count()));

foreach (var month in result)
{
    Console.WriteLine($"Month: {month.Month}, Seek days: {month.Days}");
}

// result:
// Month: 5, Seek days: 4
// Month: 6, Seek days: 4

If you need amount of seek days for specific month, make it Dictionary

var seekDays = result.ToDictionary(month => month.Month, month => month.SeekDays);

var seekDaysOfMay = seekDays.GetValueOrDefault(5, 0);
Fabio
  • 31,528
  • 4
  • 33
  • 72