C# TimeSpan 两天等于相同的时间跨度?

发布于 2024-12-07 01:52:43 字数 1177 浏览 0 评论 0原文

所以我遇到了一种情况,我需要在特定的一天触发某些事件,我想我会使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢好甜 2024-12-14 01:52:44

我认为您误解了 Days 属性。我认为你想要的是TotalDays

You're misunderstanding what the Days property is I think. I think what you want is TotalDays.

樱花坊 2024-12-14 01:52:44

如果您想获得两个日期时间之间的总天数,则需要使用 时间跨度的总天数

TotalDays、TotalMinutes、TotalHours、TotalMilliseconds 为您提供时间跨度之间的总时间量。

另一方面,天、分钟、小时和毫秒为您提供实例的当前组成部分。

因此,您需要更改示例以使用 Total 方法。

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.TotalDays);
Console.WriteLine("14 days after issuance: {0}",timeSpanDays1.TotalDays);
Console.WriteLine("\r\n");
Console.WriteLine("22 months after issuance: {0}",timeSpanMonths.TotalDays);
Console.WriteLine("22 months after issuance: {0}",timeSpanMonths1.TotalDays);
Console.WriteLine("\r\n");
Console.WriteLine("TESTING DATE {0}",DateTime.Now.AddMonths(2));

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.

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.TotalDays);
Console.WriteLine("14 days after issuance: {0}",timeSpanDays1.TotalDays);
Console.WriteLine("\r\n");
Console.WriteLine("22 months after issuance: {0}",timeSpanMonths.TotalDays);
Console.WriteLine("22 months after issuance: {0}",timeSpanMonths1.TotalDays);
Console.WriteLine("\r\n");
Console.WriteLine("TESTING DATE {0}",DateTime.Now.AddMonths(2));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文