C# TimeSpan 两天等于相同的时间跨度?
所以我遇到了一种情况,我需要在特定的一天触发某些事件,我想我会使用 TimeSpan 来获取该特定的一天,这里的想法是当 TimeSpan.Days == 0 我触发我的事件,如果它小于或大于 0 不执行任何操作... 但我认为这会起作用,但两天给了我 0 这就是我正在做的事情。
TimeSpan timeSpanDays = DateTime.Parse("12/13/2011").Subtract(DateTime.Now.AddDays(76));
TimeSpan timeSpanDays1 = DateTime.Parse("12/14/2011").Subtract(DateTime.Now.AddDays(76));
TimeSpan timeSpanMonths = DateTime.Parse("2011-11-28").Subtract(DateTime.Now.AddMonths(2));
TimeSpan timeSpanMonths1 = DateTime.Parse("2011-11-29").Subtract(DateTime.Now.AddMonths(2));
Console.WriteLine("14 days after issuance: {0}",timeSpanDays.Days);
Console.WriteLine("14 days after issuance: {0}",timeSpanDays1.Days);
Console.WriteLine("\r\n");
Console.WriteLine("22 months after issuance: {0}",timeSpanMonths.Days);
Console.WriteLine("22 months after issuance: {0}",timeSpanMonths1.Days);
Console.WriteLine("\r\n");
Console.WriteLine("TESTING DATE {0}",DateTime.Now.AddMonths(2));
这是结果
发行后14天:0
发行后14天:0
发行后22个月:0
发行后22个月:0
测试日期 2011 年 11 月 28 日上午 10:55:43
这是因为我没有剥离 DateTime 上的时间。现在?? 任何帮助或指示将不胜感激。
谢谢!!!
So I have a situation where I need to trigger certain events on a specific day, I thought I would use TimeSpan to get that specific day, the idea here is when the TimeSpan.Days == 0 I trigger my event, if its Less then or Greater then 0 do nothing...
but I thought this would work but two days give me 0
Here is what I am doing.
TimeSpan timeSpanDays = DateTime.Parse("12/13/2011").Subtract(DateTime.Now.AddDays(76));
TimeSpan timeSpanDays1 = DateTime.Parse("12/14/2011").Subtract(DateTime.Now.AddDays(76));
TimeSpan timeSpanMonths = DateTime.Parse("2011-11-28").Subtract(DateTime.Now.AddMonths(2));
TimeSpan timeSpanMonths1 = DateTime.Parse("2011-11-29").Subtract(DateTime.Now.AddMonths(2));
Console.WriteLine("14 days after issuance: {0}",timeSpanDays.Days);
Console.WriteLine("14 days after issuance: {0}",timeSpanDays1.Days);
Console.WriteLine("\r\n");
Console.WriteLine("22 months after issuance: {0}",timeSpanMonths.Days);
Console.WriteLine("22 months after issuance: {0}",timeSpanMonths1.Days);
Console.WriteLine("\r\n");
Console.WriteLine("TESTING DATE {0}",DateTime.Now.AddMonths(2));
Here Are the results
14 days after issuance: 0
14 days after issuance: 0
22 months after issuance: 0
22 months after issuance: 0
TESTING DATE 11/28/2011 10:55:43 AM
Is this because I'm not stripping the time on the DateTime.Now??
Any help or pointers would be greatly appreciated.
Thanks!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您误解了 Days 属性。我认为你想要的是
TotalDays
。You're misunderstanding what the
Days
property is I think. I think what you want isTotalDays
.如果您想获得两个日期时间之间的总天数,则需要使用 时间跨度的总天数。
TotalDays、TotalMinutes、TotalHours、TotalMilliseconds 为您提供时间跨度之间的总时间量。
另一方面,天、分钟、小时和毫秒为您提供实例的当前组成部分。
因此,您需要更改示例以使用 Total 方法。
If you want to have the total of day between two DateTime you need to use the TotalDays of TimeSpan.
TotalDays, TotalMinutes, TotalHours, TotalMilliseconds give you the total amount of time between the time span.
On the other hand, Days, Minutes, Hours and Milliseconds give you the current component of the instance.
So, you need to change you example to use Total methods.