如何使用 C# 找到一个月内的第三个星期五? [英] How to find the 3rd Friday in a month with C#?

查看:22
本文介绍了如何使用 C# 找到一个月内的第三个星期五?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个日期(DateTime 类型),我如何找到该日期所在月份的第三个星期五?

Given a date (of type DateTime), how do I find the 3rd Friday in the month of that date?

推荐答案

我没有测试过这个,但是由于第三个星期五不可能发生在当月的 15 号之前,创建一个新的 DateTime,然后增加直到你到了星期五.

I haven't tested this, but since the third Friday can't possibly occur before the 15th of the month, create a new DateTime, then just increment until you get to a Friday.

DateTime thirdFriday= new DateTime(yourDate.Year, yourDate.Month, 15);

while (thirdFriday.DayOfWeek != DayOfWeek.Friday)
{
   thirdFriday = thirdFriday.AddDays(1);
}

这篇关于如何使用 C# 找到一个月内的第三个星期五?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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