Some Use full date functions in c#.net

Some usefull date functions(Days between two dates, working days between two dates,Months between two dates,off days between two dates)
you can use this class in many projects

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;

namespace MyApp.Common
{
    /// <summary>
    /// DateTime Extension Methods
    /// </summary>
    public static class DateTimeExtensions
    {
        /// <summary>
        /// return true if day is not(sunday or Saturday)
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static bool IsWorkingDay(this DateTime date)
        {
            return date.DayOfWeek != DayOfWeek.Sunday && date.DayOfWeek!= DayOfWeek.Saturday;
        }
    }
    /// <summary>
    /// day day_name and date
    /// </summary>
    public class Date_Properties
    {
        public int Day { get; set; }
        public bool IsWorkingDay { get; set; }
        public string DayName { get; set; }
        public DateTime Date { get; set; }
        public bool IsHoliday { get; set; }
        public string DateTimeString { get; set; }
    }
    /// <summary>
    /// Date Time functions
    /// </summary>
    public class DateTime_Tools
    {
        private DateTime _startDate;
        private DateTime _endDate;
        public DateTime_Tools()
        {
            _startDate = DateTime.Today.Date;
            _endDate = DateTime.Today.Date.AddDays(30);
        }
        public DateTime_Tools(DateTime startDate, DateTime endDate)
        {
            _startDate = startDate;
            _endDate = endDate;
        }
        /// <summary>
        /// Returns All dates between startDate and EndDate
        /// </summary>
        /// <returns>List of DateTime</returns>
        public List<DateTime> AllDates()
        {
            List<DateTime> dates = new List<DateTime>();
            if (_startDate == _endDate)
            {
                dates.Add(_startDate);
                return dates;
            }
            for (var d = _startDate; d <= _endDate; d = d.AddDays(1))
            {
                dates.Add(d.Date);
            }
            return dates;
        }
        /// <summary>
        /// returns only off days between two dates (sundays and saturdays)
        /// </summary>
        /// <returns></returns>
        public List<DateTime> OffDays()
        {
          var dates=  (from date in AllDates().Where(d => d.IsWorkingDay()==false) select date).ToList();
          return dates;
        }
        /// <summary>
        /// Returns only working dates eleminates (sunday and sturday)
        /// </summary>
        /// <returns></returns>
        public List<DateTime> WorkingDates()
        {
            List<DateTime> dates = new List<DateTime>();
            for (var d = _startDate; d <= _endDate; d = d.AddDays(1))
            {
                if (d.IsWorkingDay())
                {
                    dates.Add(d.Date);
                }
            }
            return dates;
        }
        /// <summary>
        /// Number of working days between startdate and End date
        /// </summary>
        /// <returns></returns>
        public int WorkingDays()
        {
            int count = (from date in AllDates().Where(d => d.IsWorkingDay()) select date).ToList().Count;
            return count;
        }
        /// <summary>
        /// Returns number of days between two dates
        /// </summary>
        /// <returns></returns>
        public int AllDays()
        {          
           return AllDates().Count;
        }
        /// <summary>
        /// return List of Date_Properties between to dates
        /// </summary>
        /// <returns></returns>
        public List<Date_Properties> AllDays_Names()
        {
            List<Date_Properties> _toReturn = new List<Date_Properties>();
            _toReturn = (from d in AllDates() select d).ToList().Select(x => new Date_Properties()
            {
                Day = x.Day,
                IsWorkingDay = x.IsWorkingDay(),
                Date = x.Date,
                DayName = x.DayOfWeek.ToString(),
                DateTimeString = x.ToString("dd/MMM/yyyy"),
                IsHoliday = false
            }).ToList();
            return _toReturn;
        }
        /// <summary>
        /// Retuns Month and year between two dates output=> { {  "January", 2011  }, {  "February", 2011  }}
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Tuple<string, int>> MonthsBetween()
        {
            DateTime iterator;
            DateTime limit;

            if (_endDate > _startDate)
            {
                iterator = new DateTime(_startDate.Year, _startDate.Month, 1);
                limit = _endDate;
            }
            else
            {
                iterator = new DateTime(_endDate.Year, _endDate.Month, 1);
                limit = _startDate;
            }

            var dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
            while (iterator <= limit)
            {
                yield return Tuple.Create(
                    dateTimeFormat.GetMonthName(iterator.Month),
                    iterator.Year);
                iterator = iterator.AddMonths(1);
            }
        }       
       

        public DateTime StarDate { get { return _startDate; } set { _startDate = value; } }
        public DateTime EndDate { get { return _endDate; } set { _endDate = value; } }
    }
}

Post a Comment