两个日期相减 得到 月数 和 畸零天数

popeye627 2010-02-23 10:30:44
终止日期 - 起始日期 怎样快速得到 相差的月数畸零天数
就是说 终止日期 - 起始日期 = XXXX
...全文
725 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
popeye627 2010-02-26
  • 打赏
  • 举报
回复
引用 14 楼 zfj1981 的回复:
lz,如果日期是 start 2010-01-02  end 2010-02-01
按着你的算法得到的结果是 1;-1 是差一天不到一个月。
正常的结果应该是 0:30;

对着呀,吓我一跳,刚debug了下,就是0;30
sabty 2010-02-23
  • 打赏
  • 举报
回复
你好!

你可以参考我在下面地址的回复:

http://social.microsoft.com/Forums/zh-CN/visualcshartzhchs/thread/08290297-26d0-420f-ae02-af0afd9467b9
倒大霉的上帝 2010-02-23
  • 打赏
  • 举报
回复
我是新得不能再新的新手,尝试写写权当自己练手。不规格地方请指明

using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
class Program
{

static void Main(string[] args)
{
DateTime tmBegin = new DateTime(2009, 11, 11);
DateTime tmEnd = new DateTime(2010, 1, 10);

int y, m, d;
TimeSpan ts;
DateTime newBeg;

y = tmEnd.Year;
m = (tmEnd.Year - tmBegin.Year) * 12 + tmEnd.Month - tmBegin.Month;

if ((int)tmEnd.Day >= (int)tmBegin.Day)
{
d = tmEnd.Day - tmEnd.Day;
}
else
{
m -= 1;
if ((int)tmEnd.Month == 1)
{
int newM;
y -= 1;
newM = 12;
newBeg = new DateTime(y, newM, tmEnd.Day);
}
else
{
newBeg = tmEnd.AddMonths(-1);
}
ts = tmEnd - newBeg;
d = (int)ts.TotalDays-1;
}
Console.WriteLine("相差{0}个月外加{1}天", m.ToString(),d.ToString());
Console.ReadKey();
}
}
}
Jave.Lin 2010-02-23
  • 打赏
  • 举报
回复
引用 4 楼 popeye627 的回复:
to 1楼伙计 : 不能笼统的 除以30 和 除30取模 吧


对,因为不是每个月都只是30天。

这个问题还没做过,估计要考虑到万年历的东西。
别递烟哥不会 2010-02-23
  • 打赏
  • 举报
回复
不知道什么地方用这个,一般用TimeSpan就行,月是不定的·感觉这个功能有点那个·
fcfd86 2010-02-23
  • 打赏
  • 举报
回复
UP~~~~~~~~~~~~~~~~~~~~~~~~
nimingxin1987 2010-02-23
  • 打赏
  • 举报
回复
你要用月来表示时间差距本来就有问题
比如1月20日到3月21日,差的是2个月1天,实际是59天
7月20日到9月21日,差的是2个月1天,但实际是63天
popeye627 2010-02-23
  • 打赏
  • 举报
回复
to 1楼伙计 : 不能笼统的 除以30 和 除30取模 吧
Allen2064 2010-02-23
  • 打赏
  • 举报
回复
引用 1 楼 zgke 的回复:
  TimeSpan _Span = new DateTime(2010, 4, 22) - new DateTime(2010, 3, 10);


            MessageBox.Show((_Span.Days / 30).ToString()+ "月" + _Span.Days % 30 + "天");

解答的非常好
hhc123 2010-02-23
  • 打赏
  • 举报
回复
去好好查看一下msdn吧,
http://topic.csdn.net/u/20091113/10/304a9847-4645-404b-89ab-3d9a03e26739.html
zgke 2010-02-23
  • 打赏
  • 举报
回复
TimeSpan _Span = new DateTime(2010, 4, 22) - new DateTime(2010, 3, 10);


MessageBox.Show((_Span.Days / 30).ToString()+ "月" + _Span.Days % 30 + "天");
zfj1981 2010-02-23
  • 打赏
  • 举报
回复
lz,如果日期是 start 2010-01-02 end 2010-02-01
按着你的算法得到的结果是 1;-1 是差一天不到一个月。
正常的结果应该是 0:30;
popeye627 2010-02-23
  • 打赏
  • 举报
回复
谢谢各位,我的函数如下,请指教:

/// <summary>
/// 两个日期相减 得到 月数 和 畸零天数
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns></returns>
private static string DateDiff(DateTime start, DateTime end)
{
string dateDiff = null;
try
{
DateTime end1;
int date = (end - start).Days;
int date1 = 0;

if (start.Day <= end.Day)
{
end1 = new DateTime(end.Year, end.Month, start.Day);
}
else
{
end1 = new DateTime(end.Year, end.Month - 1, start.Day);
}

date1 = (end1 - start).Days;

dateDiff = ((end1.Year - start.Year) * 12 + end1.Month - start.Month).ToString() + ";" + (date - date1).ToString();
}
catch (Exception ex)
{
dateDiff = null;
}

return dateDiff;
}
足球中国 2010-02-23
  • 打赏
  • 举报
回复
TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan TimeSpan
wuyq11 2010-02-23
  • 打赏
  • 举报
回复
private string DateDiff(DateTime DateTime1,DateTime DateTime2)
{
string dateDiff=null;
try
{
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()+"秒";
}
catch
{ }
return dateDiff;
}

Int32 days =(Int32) ((TimeSpan)(dt1- dt2)).TotalDays;
MessageBox.Show(days.ToString());

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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