两个日期之间的月份开始日期和结束日期以及每个月的日期 [英] Months start and end dates and no of days in each month between two dates

查看:117
本文介绍了两个日期之间的月份开始日期和结束日期以及每个月的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下日期作为参数



开始日期:02/15/2016

结束日期:02/14/2017 < br $> b $ b

预期产量:



MonthStart MonthEnd NoOfDays

02/15/2016 02/29/2016 15

03/01/2016 03/31/2016 31

04/01/2016 04/30/2016 30

05/01/2016 05/31/2016 31

06/01/2016 06/30/2016 30

07/01/2016 07/31/2016 31

08/01/2016 08/31/2016 31

09/01/2016 09/30/2016 30

10/01 / 2016 10/31/2016 31

11/01/2016 11/30/2016 30

12/01/2016 12/31/2016 31

01/01/2017 01/31/2017 31

02/01/2017 02/14/2017 14



我尝试了什么:



我是新手,需要帮助以达到预期的输出。

解决试点

试试这个..

<前一个=c#> 公共 static void PrintDates(DateTime start,DateTime end)
{
while (开始< 结束)
{
var lastDayOfMonth = DateTime.DaysInMonth(start.Year,start.Month);
var howManyDays = lastDayOfMonth - start.Day;
var monthEndDate = start.AddDays(howManyDays);
if (end< = monthEndDate)
Console.WriteLine( {0} {1} {2},start.ToShortDateString(),end.ToShortDateString(),(end - start).TotalDays + 1 );
else
Console.WriteLine( {0} {1} {2},start.ToShortDateString(),monthEndDate.ToShortDateString(),(monthEndDate - start).TotalDays + 1 );
start = monthEndDate.AddDays( 1 );
}
}





  var  start =  new  DateTime( 2016  02  15 ); 
var end = new DateTime( 2017 02 14 );
PrintDates(开始,结束);


使用DateTime.Add等熟悉日期数学,以及TimeSpan类。对于任何给定的月份,您可以通过创建一个DateTime类来找到开始,该类具有1作为日期和给定的月份和年份。要找出那个月结束的时间,只需在一个月的第一天添加一个月,然后减去一天(加上-1天)。



这给你一个开始,结束日期,所以每天都要使用 DateTime.DayOfWeek [ ^ ]查看是否为工作日,以及是否为运行计数加1。如果你把它变成一个函数,你只需要在开始和结束日期之间每个月运行它。


I have following Dates as parameters

Start Date: 02/15/2016
End Date: 02/14/2017

Expected Output:

MonthStart MonthEnd NoOfDays
02/15/2016 02/29/2016 15
03/01/2016 03/31/2016 31
04/01/2016 04/30/2016 30
05/01/2016 05/31/2016 31
06/01/2016 06/30/2016 30
07/01/2016 07/31/2016 31
08/01/2016 08/31/2016 31
09/01/2016 09/30/2016 30
10/01/2016 10/31/2016 31
11/01/2016 11/30/2016 30
12/01/2016 12/31/2016 31
01/01/2017 01/31/2017 31
02/01/2017 02/14/2017 14

What I have tried:

I am new to this need help in getting above expected output.

解决方案

Try this..

public static void PrintDates(DateTime start, DateTime end)
{
while (start < end)
{
var lastDayOfMonth = DateTime.DaysInMonth(start.Year, start.Month);
var howManyDays = lastDayOfMonth - start.Day;
var monthEndDate=start.AddDays(howManyDays);
if(end<=monthEndDate)
Console.WriteLine("{0} {1} {2}", start.ToShortDateString(), end.ToShortDateString(), (end - start).TotalDays + 1);
else
Console.WriteLine("{0} {1} {2}", start.ToShortDateString(), monthEndDate.ToShortDateString(), (monthEndDate - start).TotalDays + 1);
start = monthEndDate.AddDays(1);               
}
}



var start = new DateTime(2016, 02, 15);
var end = new DateTime(2017, 02, 14);
PrintDates(start, end);


Get familiar with date maths using DateTime.Add etc, and the TimeSpan class. For any given month you can find the start by making a DateTime class that has 1 as the day and the given month and year. To find out when that month ends simply add one month to the first of the month then subtract one day (add -1 days).

That gives you the start and end date so go through each day and use DateTime.DayOfWeek[^] to see if it is a week day, and if it is add 1 to a running count. If you make that into a function you just need to run it for each month between the start and end dates.


这篇关于两个日期之间的月份开始日期和结束日期以及每个月的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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