2

我有一个看起来很简单的日期问题,我只是无法集中精力尝试有效地解决它......我基本上需要获取特定日期的前几个月日期。

例如:如果今天是本月的第三个星期四,我想获取上个月的第三个星期四的日期。重要的是它基于当天的数字......即:第一个星期一,第四个星期五,第二个星期三等。

完成这项工作的最佳方法是什么?

顺便说一句...如果没有等效的前几个月的一天,那很好。我可以处理。另外,目前我正在手动计算数字或天数(“星期一”、“星期二”等)来解决这个问题。我只是希望有一种更优雅的方式来做到这一点。

4

5 回答 5

1

这是我要做的:

static DateTime? GetLastMonthSameNthDayOfWeek(DateTime date)
{
    int nth = (date.Day-1) / 7; // returns 0 if 1st, 1 if 2nd...
    var prevMonthDay = date.AddMonths(-1);

    // find the first date of month having the same day of week
    var d = new DateTime(prevMonthDay.Year, prevMonthDay.Month, 1);
    while(d.Day <= 7)
    {
        if (d.DayOfWeek == date.DayOfWeek)
            break;
        d = d.AddDays(1);
    }
    // go to nth day of week
    d = d.AddDays(7 * nth);
    // if we have passed the current month, there's no nth day of week
    if (d.Month != prevMonthDay.Month)
        return null;
    return d;
}

使用示例:

// 3rd wednesday of August 2012
var a = new DateTime(2012, 8, 15);
var aPrev = GetLastMonthSameNthDayOfWeek(a);
// aPrev = July 18th 2012 (i.e. the 3rd wednesday of July 2012)

// 5th wednesday of August 2012
var b = new DateTime(2012, 8, 15);
var bPrev = GetLastMonthSameNthDayOfWeek(b);
// bPrev = null, because there's no 5th wednesday of July 2012

注意:

在一个月内获得星期几的序数位置真的很容易:

int nth = ((date.Day-1) / 7) + 1; // 1 -> 1st, 2 -> 2nd, 3 -> 3rd ...
于 2012-08-06T16:55:33.710 回答
1

由于找不到内置方法,我为 编写了这个简单的扩展方法DateTime,请查看:

public static class DateTimeExtension
{
    public static DateTime GetPositionalDate(this DateTime BaseDate, DayOfWeek WeekDay, int position)
    {
        if (position < 1)
        {
            throw new Exception("Invalid position");
        }
        else
        {
            DateTime ReturnDate = new DateTime(BaseDate.Year, BaseDate.Month, BaseDate.Day);
            int PositionControl = 1;
            bool FoundDate = false;

            while(ReturnDate.DayOfWeek != WeekDay)
            {
                ReturnDate = ReturnDate.AddDays(1);
            }

            while (!FoundDate && PositionControl <= position)
            {
                PositionControl++;

                if (PositionControl == position)
                {
                    FoundDate = true;
                }
                else
                {
                    ReturnDate = ReturnDate.AddDays(7);
                }
            }

            if (FoundDate)
            {
                return ReturnDate;
            }
            else
            {
                throw new Exception("Date not found");
            }
        }
    }
}

用法:

DateTime lastMonth = DateTime.Now.GetPositionalDate(DayOfWeek.Sunday, 2);

问候

于 2012-08-06T17:06:07.987 回答
0

默认情况下,.Net 没有一种方式可以理解日期的这种特定逻辑。因此,使用以下内容,您可以获得所需的内容:

var now = DateTime.Now.Date;

使用 DateTime.AddMonth(-1) 获取 last month

使用 DateTime.AddDays(now.Days * -1 + 1) 获取月份的第一天

使用 DateTime.DayOfWeek 确定日期并根据需要减去或增加天数

于 2012-08-06T17:04:39.103 回答
0

好的,你可以做的是确定上个月的第一天的星期几,取你想要的星期几和星期几之间的差,然后加上7 *你想要的周数(不到一周)...

// Let's get the 3rd Friday of last month:

// get starting date
DateTime date = new DateTime();

// get first day of last month
DateTime firstOfLastMonth = date.AddMonths(-1).AddDays(-1 * (date.Day + 1));

// subtract out the day of the week (get the previous Sunday, even if it is last month)
DateTime justBeforeMonth = firstOfLastMonth.AddDays((int)firstOfLastMonth.DayOfWeek);

// Add in the DayOfWeek number we are looking for
DateTime firstFridayOfMonth = justBeforeMonth.AddDays(DayOfWeek.Friday);

// if we are still in last month, add a week to get into this month
if (firstFridayOfMonth.Month != date.AddMonth(-1).Month) { firstFridayOfMonth.AddDays(7); }

// add in 2 weeks to get the third week of the month
DateTime thirdFridayOfMonth = firstFridayOfMonth.AddDays(14);
于 2012-08-06T17:08:51.253 回答
0

这是我想出的解决方案。如果这一天不存在(例如第 8 个星期六),GetDate() 将返回 null:

{
    DateTime lastMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-1);
    DateTime? date = GetDate(lastMonth.Month, lastMonth.Year, DayOfWeek.Thursday, 2);
}

private static DateTime? GetDate(int month, int year, DayOfWeek dayOfWeek, int which)
{
    DateTime firstOfMonth = new DateTime(year, month, 1);

    DateTime date;
    for (date = firstOfMonth; date.DayOfWeek != dayOfWeek; date = date.AddDays(1))
        ;

    date = date.AddDays(7 * (which - 1));

    return date.Month == month && date.Year == year ? (DateTime?)date : null;
}
于 2012-08-06T17:31:33.597 回答