指定时间段内 如何计算每月的剩余天数

zhangxun829 2012-12-28 05:08:31
聚个列子 有两个日期控件时间一个是开始时间 一个是结束时间
开始时间: 2012-12-28 结束日期:2013-12-28
如何计算才能等到每月的是剩余天数
2012-12 剩余3天
...
...
...
...
...
2013-12 剩余28天
跪求各位大神给出一个有效的方法 谢谢了
...全文
462 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangxun829 2012-12-29
  • 打赏
  • 举报
回复
引用 2 楼 Return_false 的回复:
C# code ? 1234567891011121314151617181920212223242526272829303132333435363738394041424344 DateTime begin_dt = DateTime.Parse("2012-12-28"); DateTime end_dt = DateTime.Parse(……
我想把计算结果 逐条插入到数据库中 [月份][天数] 2012-12 4 2013-01 30 ... ... 2013-12 31 我引用输出结果 只插入了最后一条记录 2013-12,31 我是新手不怎么懂
zhangxun829 2012-12-29
  • 打赏
  • 举报
回复
引用 2 楼 Return_false 的回复:
C# code ? 1234567891011121314151617181920212223242526272829303132333435363738394041424344 DateTime begin_dt = DateTime.Parse("2012-12-28"); DateTime end_dt = DateTime.Parse(……
我引用输出结果 只显示之后一个值 是怎么回事啊
zhangxun829 2012-12-29
  • 打赏
  • 举报
回复
引用 1 楼 yumen3501 的回复:
计算当前时间day 和下月第一天 的差值 C# code ? 123456789 private string DateDiff(DateTime DateTime1, DateTime DateTime2) {string dateDiff = null; TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeS……
我引用输出结果 只显示之后一个值 是怎么回事啊
  • 打赏
  • 举报
回复
DateTime begin_dt = DateTime.Parse("2012-12-28");
                DateTime end_dt = DateTime.Parse("2013-12-28");
                Dictionary<string,int> list=new Dictionary<string,int> ();
                string _preMonth=begin_dt.ToString("yyyy-MM");
                int count=0;
                while(begin_dt<=end_dt)
                {   
                    
                    string _currentMonth=begin_dt.ToString("yyyy-MM");
                    if(_preMonth==_currentMonth)
                    {
                        count+=1;
                        string _endMonth = end_dt.ToString("yyyy-MM");
                        if (_preMonth == _endMonth && begin_dt == end_dt)
                        {
                            list.Add(_preMonth, count);
                        }
                    }
                    else
                    {
                        list.Add(_preMonth,count);
                        count=1;
                        _preMonth=begin_dt.ToString("yyyy-MM");
                    }

                    begin_dt = begin_dt.AddDays(1);
                }

                /*  list
                 +		[0]	{[2012-12, 4]}	System.Collections.Generic.KeyValuePair<string,int>
                    +		[1]	{[2013-01, 31]}	System.Collections.Generic.KeyValuePair<string,int>
                    +		[2]	{[2013-02, 28]}	System.Collections.Generic.KeyValuePair<string,int>
                    +		[3]	{[2013-03, 31]}	System.Collections.Generic.KeyValuePair<string,int>
                    +		[4]	{[2013-04, 30]}	System.Collections.Generic.KeyValuePair<string,int>
                    +		[5]	{[2013-05, 31]}	System.Collections.Generic.KeyValuePair<string,int>
                    +		[6]	{[2013-06, 30]}	System.Collections.Generic.KeyValuePair<string,int>
                    +		[7]	{[2013-07, 31]}	System.Collections.Generic.KeyValuePair<string,int>
                    +		[8]	{[2013-08, 31]}	System.Collections.Generic.KeyValuePair<string,int>
                    +		[9]	{[2013-09, 30]}	System.Collections.Generic.KeyValuePair<string,int>
                    +		[10]	{[2013-10, 31]}	System.Collections.Generic.KeyValuePair<string,int>
                    +		[11]	{[2013-11, 30]}	System.Collections.Generic.KeyValuePair<string,int>
                    +		[12]	{[2013-12, 28]}	System.Collections.Generic.KeyValuePair<string,int>
 
                 */
Assassin_ 2012-12-28
  • 打赏
  • 举报
回复
计算当前时间day 和下月第一天 的差值

private string DateDiff(DateTime DateTime1, DateTime DateTime2)
 {string dateDiff = null;
 TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
 TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
 TimeSpan ts = ts1.Subtract(ts2).Duration();
 dateDiff = ts.Days.ToString()+"天"+ ts.Hours.ToString()+"小时"+ ts.Minutes.ToString()+"分钟"+ ts.Seconds.ToString()+"秒";
 return dateDiff;
 }

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧