Date Filtering - Month Verification 31 and February

Asked

Viewed 224 times

0

I have the following code:

Code example, must debug and follow the variable "lstItem" to understand its functioning

using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace TesteRotina
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Item> lstItem = new List<Item>
            {
                new Item
                {
                    IdItem = 1,
                    NomeModificador = "ITEM 1",
                    DataUltimaMontagem = new DateTime(2015, 10, 18),
                    DiaMontagem = 18,
                    TipoPeriodicidade = (int) TipoPeriodicidadeEnum.Mensal
                },
                new Item
                {
                    IdItem = 2,
                    NomeModificador = "ITEM 2",
                    DataUltimaMontagem = new DateTime(2015, 10, 31),
                    DiaMontagem = 31,
                    TipoPeriodicidade = (int) TipoPeriodicidadeEnum.Mensal
                },
                new Item
                {
                    IdItem = 3,
                    NomeModificador = "ITEM 3",
                    DataUltimaMontagem = new DateTime(2015, 10, 29),
                    DiaMontagem = 29,
                    TipoPeriodicidade = (int) TipoPeriodicidadeEnum.Mensal
                }
            };

            lstItem = FiltrarListaItem(lstItem);

        }

        private static List<Item> FiltrarListaItem(List<Item> lstItem)
        {
            List<Item> lstRetorno = new List<Item>();
            foreach (var item in lstItem)
            {
                var dataUltimaMontagem = item.DataUltimaMontagem;
                var diaMontagem = item.DiaMontagem;
                var tipoPeriodicidade = item.TipoPeriodicidade;

                switch (tipoPeriodicidade)
                {
                    case (int)TipoPeriodicidadeEnum.Mensal:
                        dataUltimaMontagem = dataUltimaMontagem.AddMonths(1);
                        break;
                    case (int)TipoPeriodicidadeEnum.Bimestral:
                        dataUltimaMontagem = dataUltimaMontagem.AddMonths(2);
                        break;
                    case (int)TipoPeriodicidadeEnum.Trimestral:
                        dataUltimaMontagem = dataUltimaMontagem.AddMonths(3);
                        break;
                    case (int)TipoPeriodicidadeEnum.Semestral:
                        dataUltimaMontagem = dataUltimaMontagem.AddMonths(6);
                        break;
                    case (int)TipoPeriodicidadeEnum.Anual:
                        dataUltimaMontagem = dataUltimaMontagem.AddYears(1);
                        break;
                }

                if (dataUltimaMontagem.Day.Equals(DateTime.Now.Day) && dataUltimaMontagem.Month.Equals(DateTime.Now.Month))
                    lstRetorno.Add(item);
            }

            return lstItem;
        }
    }

    public class Item
    {
        public int IdItem { get; set; }
        public string NomeModificador { get; set; }
        public int TipoPeriodicidade { get; set; }
        public DateTime DataUltimaMontagem { get; set; }
        public int DiaMontagem { get; set; }
    }

    public enum TipoPeriodicidadeEnum
    {
        [Description("Mensal")]
        Mensal = 1,
        [Description("Bimestral")]
        Bimestral = 2,
        [Description("Trimestral")]
        Trimestral = 3,
        [Description("Semestral")]
        Semestral = 4,
        [Description("Anual")]
        Anual = 5
    }
}

In the example, the method FiltrarListaItem, removes all items, which have DataMontagem + periodiciodade > DataAtual.
Ex.:

Datamontage = 10/19/2015 and Periodicity = Monthly.
Dating + Periodicity = 11/19/2015. This item would be disregarded as the result is greater than the current date(18/11/2015).

This is a routine that will be performed every day of the year.

How I make Items with Mount Day larger than the last day of the month to be considered and passed through the filter?
Ex.:

Measurement = 31 and the month only goes until 30.
Diamond cutting = 29, 30 or 31 and the month only goes until 28, as is the case of February.

  • The example does not work anything. I even tried to use it to try to understand where you are going, but I still don’t understand. Explain the problem better.

  • I will edit the question trying to explain the problem better.

  • I think your answer is in your question .... How to do so when the assembly day is longer than the last day of the month.. dataUltimaMontagem.Day.Equals(DateTime.Now.Day) why doesn’t it dataUltimaMontagem.Day <= DateTime.Now.Day

  • @Jonathanbarcela Se the DiaMontagem for 31/10 and the period Mensal to UltimaMontagem it has to be when? 30/11 or 01/12?

  • @jbueno It has to be the same as the last day of the month.

  • I modified your code the right way. Now it runs and it is possible to see the result. https://dotnetfiddle.net/49fWwE. I made a change, this is what you want?

  • @bigown This solution does not meet. I put an item with Dataultimamontage with the day 17/10/2015 and the mount date pro day 17 and it passed. I need to validate the assembly day too, disregarding only in the cases cited in the question(months.

  • 1

    Man, only you can understand what you’re talking about.

  • So we still don’t understand the problem. You say "Datamontage + Periodicity = 19/11/2015. This item would be disregarded because the result is greater than the current date(18/11/2015).". Day 17 is not greater, so it passes. If the problem is not well defined, no solution will suit you. It’s not a code problem, it’s a rule problem. You don’t know what the rule is. I think I’d better close the question until you know what she is.

  • @bigown Note that the method takes the day into account. This is part of the rule, which is already defined. Also consider the part of the question where it says: "How I make Items with Mount Day larger than the last day of the month to be considered and pass through the filter. "

  • 1

    @Jonathanbarcela think you better edit the question and make a list of the rules in a well organized way, really is impossible to understand what you need. The question is very confusing.

Show 6 more comments

2 answers

5


From what I understand, you need to know which the last day of the month, as you said, was given the 31st, but the month goes until the 30th etc

It has a function in DateTime that is DaysInMonth.

You must pass to this method the year and month, which it tells you if the month goes until day 31, 30, 28, 29

Example

int ultimaDiaMes = DateTime.DaysInMonth(2015, 2); // irá retornar 28

1

You can make a ExtesionMethod for his DateTime, if the date problem is recurring. Example:

using System.IO;
using System;
using ExtensionMethods;

class Program
{
    static void Main()
    {
        DateTime hoje = DateTime.Now;

        Console.WriteLine(string.Format("Hoje é {0}", hoje));
        Console.WriteLine(string.Format("Último dia é {0}", hoje.LastDayOfThisMonth()));
    }
}

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static DateTime LastDayOfThisMonth(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1);
        }
    }   
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.