如何通过整数共享DateTime(C Sharp) [英] How do I share DateTime by an Integer (C Sharp)

查看:81
本文介绍了如何通过整数共享DateTime(C Sharp)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取用户打开程序的平均时间。但是我遇到了一个我无法解决的问题。



Hi, I am trying to get the Average Time the User has the Program open. But I have a Problem I can't solve.

TimeDate timePerOpen = new DateTime(Settings.Default.userTimeCount_ProgramOpen.Ticks / Settings.Default.userActionCound_ProgramStart));





两个Settings.Settings完全有效很好,我查了一下。但我认为他们有分歧的问题。 timePerOpen 是完全不现实的,比如2分钟的总开放时间= 56年的平均开放时间。



userActionCound 永远不会 null 或0



感谢您的帮助;)



Both Settings.Settings work totally fine, I checked that. But I think that they have problems to divide. timePerOpen is totally unrealistic like 2 min total open time = 56 years average open time.

userActionCound is never null or 0

Thanks for your help ;)

推荐答案

在进行计算时尝试使用 TimeSpan 而不是 DateTime 。正如其他人所指出的那样,人们需要施展分裂以便一切顺利。以下是演员的固定解决方案。



使用您的输入:



Try using TimeSpan instead of DateTime when doing the computation. As someone else pointed out, one needs to cast the division so that everything works out. Here is the fixed solution with the cast.

Using your inputs:

Int32 count = 4;
TimeSpan totalOpened = new TimeSpan(0, 0, 3);
TimeSpan t = new TimeSpan((long)(totalOpened.Ticks / count));
Console.WriteLine("Avg. time open = {0:d.hh:mm:ss.ff}", t.ToString());





开启时间= 4

总计时间= 3秒



输出将是:





Times opened = 4
Total time = 3 seconds

The output will be:

Avg. time open = 00:00:00.7500000





平均时间= 750毫秒



Average time = 750 milliseconds


这篇关于如何通过整数共享DateTime(C Sharp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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