如何使用 +- 10 秒缓冲区获取日期时间?
我有:
jobElement.CreationDate = jobElement.CreationDate + TimeSpan.FromHours(24.0);
我想要的不是严格的 24 小时,而是 +- 10 秒的缓冲区。像 23.59.10 和 00.00.10 一样,
用 c# 可以达到这个目的吗?
i have:
jobElement.CreationDate = jobElement.CreationDate + TimeSpan.FromHours(24.0);
i would like to have not strictly 24 hours, but with +- 10 seconds Buffer. like 23.59.10 and 00.00.10
hot to reach that with c#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这将以相同的概率生成 CreationDate + 23:50 和 CreationDate + 24:10:
This will generate CreationDate + 23:50 and CreationDate + 24:10 with equal probability:
你需要做什么?
如果您需要进行任何比较,请创建带有覆盖相等运算符的自定义类。
What do you need to do with that?
If you need to any comparison create custom class with overwritten equality operators.
我不是 100% 确定你想要什么,但我会尝试一下。
这两个时间现在相隔 24 小时零 8 秒。
然后,如果您想查看它们是否“几乎”24 小时开放,您可以执行以下操作:
I'm not 100% sure what you want here but I'll give it a shot
These two are now 24 hours and 8 seconds apart.
Then if you want to see if they are "almost" 24 hour appart, you can do something like this:
当前日期的第一次 (00.00.00) -/+ 10 秒将是:
是这样吗?
First time (00.00.00) of current date -/+ 10 secs would be:
Is that it?
我将添加这个变体。它与其他的不同,因为它不是“基于秒”而是基于“刻度”(刻度是 TimeSpan/DateTime 可以计算的最小时间)。
Random
类中存在限制(它可以不会生成一个 long,并且生成一个“约束”long(maxValue = x 的 long)仅基于 Random 类是不平凡的,因此这将工作长达 3 分钟和一些“缓冲”(214 秒)更准确地说)。I'll add this variant. It's different from others because it isn't "second based" but "tick" based (the tick is the smallest time that a TimeSpan/DateTime can compute)
There are limits in the
Random
class (it can't generate a long, and generating a "constrained" long (a long with maxValue = x) is non-trivial based only on the Random class, so this will work for up to 3 minutes and something of "buffer" (214 seconds to be more exact).如果您想要 +/- 10 之间的所有数字
If you want +/- 10 with all numbers between