时间跨度问题 [英] timespan issue

查看:65
本文介绍了时间跨度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个TimeSpan,我可以从日期开始减去日期。


TimeSpan ts(Dim ts作为TimeSpan)

ts = parsedDate.Subtract(DateTime.Now)

i获取Hours和TotalHours的不同值。在我的情况下,我是

geting -15和-39。

两者之间的差异是24(我知道很明显)。

另外我可以告诉你一个用00:00而另一个用23:59。


但为什么??

i使用.net 1.1


提前感谢

解决方案

parez,


你将不得不提供一个实际的代码示例(它现在也不能有现金

,因为那不是一个非常明确的例子,因为现在

属性总是在变化)因为根据你所展示的

几乎无法判断。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" parez" < ps ***** @ gmail.com在留言中写道

news:11 ********************** @ n15g2000prd。 googlegr oups.com ...


大家好,


我有一个TimeSpan,我可以从一个约会。


TimeSpan ts(Dim ts As TimeSpan)

ts = parsedDate.Subtract(DateTime.Now)


i为Hours和TotalHours获取不同的值。在我的情况下,我是

geting -15和-39。

两者之间的差异是24(我知道很明显)。

另外我可以告诉你一个人使用00:00而另一个使用23:59。


但是为什么?


i使用.net 1.1


提前感谢



2007年6月13日星期三12:42:55 -0700,parez< ps ***** @ gmail.comwrote:


[...]

i得到不同的值小时和总时数。在我的情况下,我是

geting -15和-39。

两者之间的差异是24(我知道很明显)。

另外我可以告诉他们一个人使用00:00而另一个人使用23:59。


但是为什么?



我同意Nicholas的观点,为了获得最佳效果,你应该发布一个

简洁但完整的代码示例,可以可靠地再现

的行为。然后有人可以用非常具体的方式回答这个问题。


这就是说,简单的答案是,小时和总时间是两个完全不同的东西。如果您将阅读每个文档,

您会发现小时数限制在-23和23之间。在

其他的话,它'是小时的地方一段完整的时间。在

另一方面,TotalHours _是整个时间段,表示为

小时。


它是排序就像说完需要花费90分钟(其中90分是总分钟数)之间的差异,相比之下它将在1小时30分钟内完成
完成(其中30是

时间跨度的分钟位置。


Pete




string dt =" 06/12 / 2007";


TimeSpan ts;

String [] expectF = new string [] {"%M /%d / yyyy","%M /%d /

yyyy%H:mm" };

System.Globalization.CultureInfo ci =

System.Globalization.CultureInfo.CurrentCulture;


DateTime parsedDate = DateTime。 ParseExact(dt,expectF,ci,

System.Globalization.DateTimeStyles.None);


ts = parsedDate.Subtract(DateTime.Now);

Console.WriteLine(" Total HOurs" + ts.TotalHours);

Console.WriteLine(" HOurs" + ts.Hours);

Hi all,

I have a TimeSpan which i get my subtracing a date from a date.

TimeSpan ts (Dim ts As TimeSpan)
ts = parsedDate.Subtract(DateTime.Now)

i get different values for Hours and TotalHours. In my case i was
geting -15 and -39.
The difference between two is 24 (i know it is obvious).
Also i can alsp tell that one use 00:00 and the other uses 23:59.

but why??
i using .net 1.1

thanks in advance

解决方案

parez,

You are going to have to give an actual code sample (it can''t have Now
in it either, since that would not be a very definitive example, as the Now
property is always changing) as it''s nearly impossible to tell based on what
you have shown.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"parez" <ps*****@gmail.comwrote in message
news:11**********************@n15g2000prd.googlegr oups.com...

Hi all,

I have a TimeSpan which i get my subtracing a date from a date.

TimeSpan ts (Dim ts As TimeSpan)
ts = parsedDate.Subtract(DateTime.Now)

i get different values for Hours and TotalHours. In my case i was
geting -15 and -39.
The difference between two is 24 (i know it is obvious).
Also i can alsp tell that one use 00:00 and the other uses 23:59.

but why??
i using .net 1.1

thanks in advance



On Wed, 13 Jun 2007 12:42:55 -0700, parez <ps*****@gmail.comwrote:

[...]
i get different values for Hours and TotalHours. In my case i was
geting -15 and -39.
The difference between two is 24 (i know it is obvious).
Also i can alsp tell that one use 00:00 and the other uses 23:59.

but why??

I agree with Nicholas that for best results, you should post a
concise-but-complete sample of code that reliably reproduces the
behavior. Then someone can answer the question in a very specific way.

That said, the simple answer is that Hours and TotalHours are two
completely different things. If you''ll read the documentation for each,
you''ll find that Hours is restricted to values between -23 and 23. In
other words, it''s the "hours place" of a complete span of time. On the
other hand, TotalHours _is_ the complete span of time, represented as
hours.

It''s sort of like the difference between saying that something takes 90
minutes to complete (where 90 is the total minutes), versus that it will
complete in 1 hour and 30 minutes (where 30 is the "minutes place" of the
time span).

Pete



string dt = "06/12/2007";

TimeSpan ts;
String[] expectF = new string[] { "%M/%d/yyyy", "%M/%d/
yyyy %H:mm" };
System.Globalization.CultureInfo ci =
System.Globalization.CultureInfo.CurrentCulture;

DateTime parsedDate = DateTime.ParseExact(dt, expectF, ci,
System.Globalization.DateTimeStyles.None);

ts = parsedDate.Subtract(DateTime.Now);
Console.WriteLine("Total HOurs" + ts.TotalHours);
Console.WriteLine("HOurs" + ts.Hours);


这篇关于时间跨度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆