找到两个日期之间一个月的最后一个星期四C# [英] Find the last thursday of a month between two dates C#

查看:164
本文介绍了找到两个日期之间一个月的最后一个星期四C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两天之间的每个月的最后一个星期四。我的一些结果是正确的,其他的是星期五晚些时候发生的。以下是我的代码和结果。

任何帮助将不胜感激。



我尝试过:



  public   class  BusinessWeekDays 
{
public DateTime StartDateMonth;
public DateTime EndMonth;
}

静态 void Main( string [] args)
{
var StartDate = DateTime.Parse( 04/25/2019);
var SeriesEndDate = DateTime.Parse( 2019\" 年9月30日);

var months = new 列表< businessweekdays>();
var 每月= 1 ;
for var i = StartDate; i < SeriesEndDate; i = i.AddMonths(每月))
{
const int thursday = 4 ;
const int dayDiff = 7 - 星期四;

var Day = DateTime.DaysInMonth(i.Year,i.Month);
var DayOfWeek =( int new DateTime(i.Year,i.Month,Day).DayOfWeek;

var _dtNew = new DateTime(i.Year,i.Month, Day).AddDays( - (DayOfWeek < thursday?DayOfWeek + dayDiff:DayOfWeek - thursday));
var e = _dtNew.AddMonths(+1).AddDays(-2);
months.Add( new BusinessWeekDays {StartDateMonth = _dtNew,EndMonth = e});
Console.WriteLine(月);
}
}
}









结果:

------------

 [0] 
StartDateMonth {4/25/2019 12:00:00 AM} System.DateTime ----->更正日期和日期
EndMonth {5/23/2019 12:00:00 AM} System.DateTime
[1]
StartDateMonth {5/30/2019 12:00:00 AM} System.DateTime -----错误的日期和星期五
EndMonth {6/28/2019 12:00 :00 AM} System.DateTime

[2]
StartDateMonth {6/27/2019 12:00:00 AM} System.DateTime
EndMonth {7/25/2019上午12:00:00} System.DateTime ----->正确的日期和日期

[3]
StartDateMonth {7/25/2019 12:00:00 AM} System.DateTime
EndMonth {8/23/2019 12:00 :00 AM} System.DateTime ---不正确的日期和日期

[4]
StartDateMonth {8/29/2019 12:00:00 AM} System.DateTime
EndMonth {9/27/2019 12:00:00 AM} System.DateTime ----日期和日期不正确

解决方案

A linq(lambda)查询发现两个日期之间的星期四的星期四:



  int  datediff =(SeriesEndDate  -  StartDate).Days + 1; 
var lastThursdaysBetweenDates = Enumerable.Range( 0 ,datediff)
。选择( x => StartDate.AddDays(x))
.Where(d = > d.DayOfWeek == DayOfWeek.Thursday)
。 GroupBy(d => d.Month)
。选择(grp => grp.Max(d => d));





祝你好运!


看看这里:DateTime Extensions使一些简单的任务更容易阅读 [ ^ ] - 它可能无法准确解决您的问题,但它会为您提供一个工作基础 - 它包括一个最后一天...的一天方法,您可以很容易地修改。

Quote:

我的一些结果是正确的,其他的是星期五晚些时候发生的。



可能你对结果正确性的评价也是错误的。

引用:

 StartDateMonth {4/25/2019 12:00:00 AM} System.DateTime ----->更正日期和日期
EndMonth {5/23/2019 12:00:00 AM} System.DateTime



5月的最后一个星期四是30,而不是23!

查看此部分

  var  e = _dtNew.AddMonths(+1 ).AddDays(-2); 



月份有不同的天数!



你的代码没有表现出你期望的方式,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。 br />
调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不是'知道你的代码应该做什么,它没有发现错误,它只是通过向你展示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



在Visual Studio中调试C#代码 - YouTube [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


I am trying to get the last Thursday of every month between two days. Some of my results are correct, others are occurring a day late on Fridays. Below is my code and the results.
Any help will be appreciated.

What I have tried:

public class BusinessWeekDays
        {
            public DateTime StartDateMonth;
            public DateTime EndMonth;
        }      
     
        static void Main(string[] args)
        {
            var StartDate = DateTime.Parse("04/25/2019");
            var SeriesEndDate = DateTime.Parse("09/30/2019");

            var months = new List<businessweekdays>();
            var Monthly = 1;
            for (var i = StartDate; i < SeriesEndDate; i = i.AddMonths(Monthly))
            {
                const int thursday = 4;
                const int dayDiff = 7 - thursday;
                
                var Day = DateTime.DaysInMonth(i.Year, i.Month);
                var DayOfWeek = (int)new DateTime(i.Year, i.Month, Day).DayOfWeek;
                
                var _dtNew = new DateTime(i.Year, i.Month, Day).AddDays(-(DayOfWeek < thursday ? DayOfWeek + dayDiff : DayOfWeek - thursday));
                var e = _dtNew.AddMonths(+1).AddDays(-2);
                months.Add(new BusinessWeekDays{ StartDateMonth = _dtNew, EndMonth = e});
                Console.WriteLine(months);
            }
           }
        }





Results:
------------

[0]	 		
		StartDateMonth	{4/25/2019 12:00:00 AM}	System.DateTime  ----->Correct Date and Day
		EndMonth	{5/23/2019 12:00:00 AM}	System.DateTime
		[1]	 
		StartDateMonth	{5/30/2019 12:00:00 AM}	System.DateTime -----Incorrect Date and Day Friday 
		EndMonth	{6/28/2019 12:00:00 AM}	System.DateTime
		
		[2]	
        StartDateMonth	{6/27/2019 12:00:00 AM}	System.DateTime		
		EndMonth	{7/25/2019 12:00:00 AM}	System.DateTime -----> Correct Date and day 
		
		[3]	 
		StartDateMonth	{7/25/2019 12:00:00 AM}	System.DateTime
		EndMonth	{8/23/2019 12:00:00 AM}	System.DateTime ---Incorrect Date and day
		
		[4]	 
		StartDateMonth	{8/29/2019 12:00:00 AM}	System.DateTime
		EndMonth	{9/27/2019 12:00:00 AM}	System.DateTime ---- Incorrect Date and Day

解决方案

A linq (lambda) query which "finds" last thursdays of months between two dates:

int datediff = (SeriesEndDate - StartDate).Days+1;
var lastThursdaysBetweenDates = Enumerable.Range(0, datediff)
    .Select(x=>StartDate.AddDays(x))
    .Where(d=> d.DayOfWeek==DayOfWeek.Thursday)
    .GroupBy(d=>d.Month)
    .Select(grp=>grp.Max(d=>d));



Good luck!


Have a look here: DateTime Extensions to Make Some Simple Tasks a Little More Readable[^] - it may not solve your problem exactly, but it'll give you a base to work from - it includes a "last ...day of the month" method which you could modify quite easily.


Quote:

Some of my results are correct, others are occurring a day late on Fridays.


May be your evaluation of correctness of result is wrong too.

Quote:

StartDateMonth {4/25/2019 12:00:00 AM} System.DateTime ----->Correct Date and Day
EndMonth {5/23/2019 12:00:00 AM} System.DateTime


Last thursday of May is 30, not 23 !
Check this part

var e = _dtNew.AddMonths(+1).AddDays(-2);


Months have different number of days!

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于找到两个日期之间一个月的最后一个星期四C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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