从星期几获取每月的所有日期 [英] Get all the dates of the month from Day of week

查看:89
本文介绍了从星期几获取每月的所有日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取指定月份和年份中特定日期的所有发生日期。

Is there any way to get all the occuring date for the particular day of the specified month and year..

例如:-如果我有Day 星期四,那么我需要2016年12月的所有日期。然后集合应包含日期1/12 / 2016、8 / 12 / 2016、15 / 12 / 2016、22 / 12 / 2016、29 / 12/2016

For example:- If I have Day Thursday then I need all the dates of December 2016. Then collection should contain dates 1/12/2016, 8/12/2016, 15/12/2016, 22/12/2016, 29/12/2016.

推荐答案

您可以创建一个函数,以根据特定年份的日期(例如

You can create a function to get the list of dates of the month based on day of a particular year like

public static List<DateTime> GetDates(int year, int month,string day)
    {
       return Enumerable.Range(1, DateTime.DaysInMonth(year, month))
            .Where(d=>new DateTime(year, month, d).ToString("dddd").Equals(day))            
           .Select(d => new DateTime(year, month, d)).ToList(); 
    }

现在像

var dates=GetDates(2016,12,"Thursday");
    foreach(var d in dates){
    Console.WriteLine(d.ToString());
    }

输出将为


2016/12/1上午12:00:00

12/1/2016 12:00:00 AM

2016/12/8 12:00 :00 AM

12/8/2016 12:00:00 AM

2016/12/15 12:00:00 AM

12/15/2016 12:00:00 AM

2016/12/22 12 :00:00 AM

12/22/2016 12:00:00 AM

2016年12月29日12:00:00 AM

12/29/2016 12:00:00 AM

现在您已经有了基于一天的完整日期列表。您可以根据需要进一步使用它。

Now you have complete list of dates based on a day. You can further use it based on your requirements.

这篇关于从星期几获取每月的所有日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆