本文介绍了从开始日期查找每个星期五到今年年底的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我回来了另一个莫名其妙的日期时间问题。

So I'm back with another baffling DateTime question.

在C#中,我如何从一个起始日期返回(天),每星期五( DateTime.Now ),直到当年年底?

In C#, how would I return the (day) for every Friday from a start date (DateTime.Now) until the end of the current year?

因此,举例来说,今天是周五19日,它会返回时,26日,2,9,16,23,30,7,等等。

So for example, today being Friday the 19th, it would return, 26, 2, 9, 16, 23, 30, 7, etc.

推荐答案

工作的呢?

static IEnumerable<DateTime> GetFridays(DateTime startdate, DateTime enddate)
{
    // step forward to the first friday
    while (startdate.DayOfWeek != DayOfWeek.Friday)
        startdate = startdate.AddDays(1);

    while (startdate < enddate)
    {
        yield return startdate;
        startdate = startdate.AddDays(7);
    }
}

这篇关于从开始日期查找每个星期五到今年年底的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:36