Example #1
0
 /// <summary>
 /// Vérifie que les dates de début et de fin sont valides
 /// </summary>
 private void DatesValidation()
 {
     if (dateDebutProjet.Value > dateFinProjet.Value)
     {
         dateFinProjet.Value = dateDebutProjet.Value.AddDays(1);
     }
     else if (dateDebutProjet.Value.Month > 8)
     {
         if (DateTimeSpan.CompareDates(dateDebutProjet.Value, dateFinProjet.Value).Months > (18 - dateDebutProjet.Value.Month))
         {
             dateFinProjet.Value = dateDebutProjet.Value.AddMonths(18 - dateDebutProjet.Value.Month);
         }
     }
     else if (dateDebutProjet.Value.Month < 7)
     {
         if (DateTimeSpan.CompareDates(dateDebutProjet.Value, dateFinProjet.Value).Months > (7 - dateDebutProjet.Value.Month))
         {
             dateFinProjet.Value = dateDebutProjet.Value.AddMonths(7 - dateDebutProjet.Value.Month);
         }
     }
 }
Example #2
0
        /// <summary>
        /// Compare deux dates entre elles et renvoie un objet comportant le temps qui les sépare en secondes, minutes, heures, jours, mois et années
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns>un écart de date</returns>
        public static DateTimeSpan CompareDates(DateTime date1, DateTime date2)
        {
            if (date2 < date1)
            {
                var sub = date1;
                date1 = date2;
                date2 = sub;
            }

            DateTime current = date1;
            int      years   = 0;
            int      months  = 0;
            int      days    = 0;

            Phase        phase       = Phase.Years;
            DateTimeSpan span        = new DateTimeSpan();
            int          officialDay = current.Day;

            while (phase != Phase.Done)
            {
                switch (phase)
                {
                case Phase.Years:
                    if (current.AddYears(years + 1) > date2)
                    {
                        phase   = Phase.Months;
                        current = current.AddYears(years);
                    }
                    else
                    {
                        years++;
                    }
                    break;

                case Phase.Months:
                    if (current.AddMonths(months + 1) > date2)
                    {
                        phase   = Phase.Days;
                        current = current.AddMonths(months);
                        if (current.Day < officialDay && officialDay <= DateTime.DaysInMonth(current.Year, current.Month))
                        {
                            current = current.AddDays(officialDay - current.Day);
                        }
                    }
                    else
                    {
                        months++;
                    }
                    break;

                case Phase.Days:
                    if (current.AddDays(days + 1) > date2)
                    {
                        current = current.AddDays(days);
                        var timespan = date2 - current;
                        span  = new DateTimeSpan(years, months, days, timespan.Hours, timespan.Minutes, timespan.Seconds, timespan.Milliseconds);
                        phase = Phase.Done;
                    }
                    else
                    {
                        days++;
                    }
                    break;
                }
            }

            return(span);
        }