获取两个给定日期之间的所有月份名称

本文关键字:之间 日期 两个 获取 | 更新日期: 2023-09-27 18:30:33

我正在尝试创建一个函数,该函数在 c#
中给出两个日期之间的所有月份名称

List<string> liMonths = MyFunction(date1,date2);

我的函数是

MyFunction(DateTime date1,DateTime date2)
{
//some code
return listOfMonths;
}

你能帮我怎么做吗

获取两个给定日期之间的所有月份名称

还没有 linq 解决方案?

var start = new DateTime(2013, 1, 1);
var end = new DateTime(2013, 6, 22);
// set end-date to end of month
end = new DateTime(end.Year, end.Month, DateTime.DaysInMonth(end.Year, end.Month));
var diff = Enumerable.Range(0, Int32.MaxValue)
                     .Select(e => start.AddMonths(e))
                     .TakeWhile(e => e <= end)
                     .Select(e => e.ToString("MMMM"));

您可以将 Linq 与帮助程序函数一起使用

IEnumerable<DateTime> GetDates(DateTime date1, DateTime date2) 
{
    while (date1 <= date2) 
    {
        yield return date1;
        date1 = date1.AddMonths(1);
    }
    if (date1 > date2) 
    { 
        // Include the last month
        yield return date1;
    }
}

那么MyFunction可以是以下之一

1) 包括年份名称

List<string> MyFunction(DateTime date1, DateTime date2) {
    return GetDates(date1,date2).Select(x => x.ToString("MMMM yyyyy")).ToList();
}

2)只有月份名称,有重复项

List<string> MyFunction(DateTime date1, DateTime date2) {
  return GetDates(date1,date2).Select(x => x.ToString("MMMM")).ToList();
}

3) 不同的月份名称

List<string> MyFunction(DateTime date1, DateTime date2) {
    return GetDates(date1,date2).Select(x => x.ToString("MMMM")).Distinct().ToList();
}

创建一个循环开始日期 1 直到日期 2。为循环的每一步添加一个月,并在当月填写返回变量。

我尝试用元语言写下您的目标:

DateTime currDate = date1
List myList = new List();
while (currDate < date2) {
    myList.add(getMonthName(currDate));
    currDate = currDate.addMonth(1);
}
DateTime from;
DateTime to;

var source = from.Month > to.Month ?
               Enumerable.Range(from, 12).Concat(Enumerable.Range(1, to.Month))
             : Enumerable.Range(1, 12)
               .SkipWhile(m => m >= from.Month)
               .TakeWhile(m => m <= to.Month)
var monthes = source
     .Select(m => CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m));
    List<string> MonthNames(DateTime date1, DateTime date2)
    {
        var monthList = new List<string>();
        while (date1 < date2)
        {
            monthList.Add(date1.ToString("MMMM/yyyy"));
            date1 = date1.AddMonths(1);
        }
        return monthList;
    }
static IEnumerable<DateTime> monthsBetween(DateTime d0, DateTime d1)
{
    return Enumerable.Range(0, (d1.Year - d0.Year) * 12 + (d1.Month - d0.Month + 1))
                     .Select(m => new DateTime(d0.Year, d0.Month, 1).AddMonths(m));
}
private static List<string> MyFunction(DateTime date1, DateTime date2)
        {
            var listOfMonths=new List<string>();
            if (date1.CompareTo(date2) == 1)
            {
                var temp = date2;
                date2 = date1;
                date1 = temp;
            }
            var mosSt = date1.Month;
            var mosEn = date2.Month;
            var yearSt = date1.Year;
            var yearEn = date2.Year;
            while (mosSt < mosEn || yearSt < yearEn)
            {
                var temp = new DateTime(yearSt, mosSt, 1);
                listOfMonths.Add(temp.ToString("MMMM", CultureInfo.InvariantCulture));
                mosSt++;
                if (mosSt < 13) continue;
                yearSt++;
                mosSt = 1;
            }
            return listOfMonths;
        }