确定一个月的第一天、一个月的最后一天等的函数。
我正在创建一个调度程序,需要能够在 C# 中执行以下操作:
- 查找 2012 年 6 月的第一个星期二
- 查找 2008 年 3 月的最后一个
- 星期五 查找 2013 年 1 月的每个星期六
- 查找 2009 年 7 月的第 3 个星期五
- 查找 2009 年 7 月的每个星期六接下来 3 个月
- 查找 2018 年 3 月的每一天
结果应以 DateTime
或 List
形式返回。
I'm creating a scheduler and need to be able to do the following in C#:
- Find the 1st Tuesday of June 2012
- Find the last Friday of March 2008
- Find every Saturday in January 2013
- Find the 3rd Friday in July 2009
- Find every Saturday over the next 3 months
- Find every day in March 2018
The results should come back as DateTime
or List<DateTime>
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
以下是查找给定月份中一周的第一天/最后一天的方法:
从这些方法中,您可以轻松找到其他问题的答案...只需添加 7 天即可找到您要查找的日期的下一次出现编辑
:更多地考虑一下,以扩展方法的形式提供它会非常方便,例如:
这是实现:
它可能比我建议的第一种方法更灵活......有了它,您可以轻松地做这样的事情:
Here are methods to find the first/last specified day of week in a given month:
From those, you can easily find the answers to the other questions... just add 7 days to find the next occurence of the day you're looking for
EDIT: thinking more about it, it would be pretty handy to have that in the form of extension methods, such as :
Here is the implementation :
It's probably more flexible than the first methods I suggested... With that, you can easily do things like that :
受这个问题的启发,制作一些东西来允许您将日期视为序列并使用 LINQ 查询它们似乎是一件有趣的事情。我制作了一个简单的项目,可以从 GitHub 此处下载该项目。不知道是否有必要将其添加到源代码管理中,因为我怀疑我是否会处理它,但必须将其上传到某个地方并将其上传到 RapidShare 似乎有点狡猾:)
您的第一个问题用我的“Linq”解决了-to-DateTime”:
此示例中未显示的是您可以选择查询的粒度。这意味着如果您这样做:
您将以一年为增量迭代日期。如果您未指定任何内容,则默认值为 AsDays()。
该项目非常简单,没有评论或单元测试,但我希望这仍然可以帮助您。如果没有,那么无论如何,写起来很有趣:)
Inspired by this question it seemed like a fun thing to make something that allows you to treat dates as sequences and query them with LINQ. I've made a simple project that can be downloaded here from GitHub. Don't know if it was necessary to add it to source control as I doubt I'll be working on it, but had to upload it somewhere and uploading it to RapidShare seemed a little dodgy :)
Your first questions solved with my "Linq-to-DateTime":
Not shown in this example is that you can choose the granularity of your queries. Meaning that if you do this:
You will iterate through the dates with an increment of a year. The default is AsDays() if you don't specify anything.
The project is pretty barebone, no commentary or unit tests, but I hope this can still help you. If not, then it was fun writing anyway :)
以下是如何处理每月第一天的示例。
你这样称呼它,
所以你明白了。您将必须执行各种功能才能获得您想要实现的目标。
Here is an example on how to do the first day of a month.
You call it like
So you get the idea. You will have to do various functions do get what you want to achieve.
YO YO YO - 你们把这件事搞得太难了:
int DaysinMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
YO YO YO - You guys are making this too hard:
int DaysinMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
当我编写调度程序时,我几乎复制了 SQL Server sysschedules 表
http://msdn .microsoft.com/en-us/library/ms178644.aspx
使用诸如频率类型、频率间隔等之类的东西...确实可以轻松计算代码中的值,但我猜测有一个存储过程为你做这件事。我现在正在寻找那个。
When I wrote a scheduler I pretty much copied SQL Servers sysschedules table
http://msdn.microsoft.com/en-us/library/ms178644.aspx
Using things like Frequency Type, Frequency Interval, etc... really made it easy to compute the values in code, but I am guessing there is a sproc to do it for you. I am searching for that now.
是的,
.NET Framework
将支持这一点,不会出现任何问题;)但认真地说,您应该能够编写相当容易实现这一点的代码。如果没有,请在遇到困难时提出问题,我们会提供帮助。但您不需要任何外部程序集 - 只需使用标准 C# 类 =)
Yes, the
.NET Framework
will support that without any problems ;)But seriously - you should be able to write code that does that fairly easily. If not, ask a question when you get stuck, and we'll help out. But you won't need any external assemblies - just go with a standard C# class =)