两个日期之间的月份开始日期和结束日期以及每个月的日期

编程入门 行业动态 更新时间:2024-10-24 18:27:08
本文介绍了两个日期之间的月份开始日期和结束日期以及每个月的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下日期作为参数 开始日期:02/15/2016 结束日期:02/14/2017 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 我尝试了什么: 我是新手,需要帮助以达到预期的输出。

解决试点

试试这个.. 公共 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.

更多推荐

两个日期之间的月份开始日期和结束日期以及每个月的日期

本文发布于:2023-10-30 23:46:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   每个月   两个   结束

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!