计算年和月中的天数?
如何在c#中计算年和月中的天数? 每个例子:
如果 1. days = 385
然后我需要显示Year= 1.1
(即1年1月) 2. days= 234
那么我需要显示year =0.7
(即0年7个月)
我们如何在c#中计算?
我做了 days=234 /365
,结果是 0.64(即 0 年 6 个月)。但实际上是7个月。
如何获得准确的年份和月份。
How to calculate days in years and months in c#?
per example :
If
1. days = 385
then I need to display Year= 1.1
(i.e. 1 year 1 month)
2. days= 234
then I need to display year =0.7
(i.e 0 year 7 months)
How can we calculate in c#?
I did for days=234 /365
and result is coming 0.64 (i.e. 0 year 6 months). But actually it is 7 months.
How to get accurate year and months.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以这样做:
这是因为您获得了 0.64,即 0.64 年。如果你想要几个月,你需要乘以 12。
在上面,你会得到 0 年零 7 个月...话虽这么说,我不确定你到底想如何格式化它:
只是请注意,这将在 3.11 中执行 11 个月,这会很奇怪,尽管这是您的要求。
另外,如果您想让它非常通用,您可能需要使用 365.25 而不是 365 来表示单个 儒略年,因为它将帮助您减少因闰年而导致的问题。
You can do:
This is because you were getting 0.64, which is 0.64 years. If you want months, you'd need to multiply that times 12.
In the above, you'll get 0 years and 7 months... That being said, I'm not sure exactly how you want to format this:
Just be aware that this will do 3.11 for 11 months, which is going to be odd, though it was your requirement.
Also, if you want to have this be very general, you might want to use 365.25 instead of 365 to represent a single Julian Year, as it will help you reduce issues due to leap years.
如果您不知道实际日期,那么您可以估计:
其中 % 是模数
If you don't know the actual dates, then you could estimate:
where % is modulo
假设一个月正好是一年的十二分之一,并且您想忽略部分月份(根据您的说法,您期望示例中的 7 个月份为 7.688 个月),那么:
Assuming a month of exactly one-twelfth of a year, and that you want ignore partial months (based on your saying you expect 7 from your example with 7.688 months, then:
您确定需要这种格式吗?结果——至少根据当前提供的信息——将是模糊的,因为几个月的长度不一致。
考虑几个替代方案:
仅表示年份,例如 1.25 表示“1 又四分之一年”。这不会混合月份和年份,因此仍然很简单,因为一年是 365 天(闰年除外)。它还消除了歧义,例如“1.10 == 1.1 吗?”
使用具体的开始日期,这将允许您使用强类型日期。您可以轻松地使用带有日期格式参数的
.ToString()
来快速准确地获取结果。Are you sure you want this format? The result--at least with the current information provided--will be fuzzy since months are inconsistent lengths.
Consider a couple alternatives:
A Year-only representation, such as 1.25 meaning "1 and one quarter years". This doesn't mix months and years, and as such remains simple since a year is 365 days (except for leap years). It also removes ambiguity such as "does 1.10 == 1.1?"
Using a concrete start date which would allow you to use strongly-type dates. You could easily use a
.ToString()
with date-formatting arguments to quickly and accurately get your result.让我们做一些计算。
我没有时间证明其他数字,但你可以尝试绕过这个公式。
Let do some calculations.
I have no time to prove other number but you can try around this formula.
我的建议是使用 DateTime.AddDays:它将为您提供所需的一切。您还可以在那里添加其他时间单位:
My suggestion would be to use DateTime.AddDays: it will give you all you need. You can also add other time units there:
伪代码
Pseudo code
在 VB 中
答案 = .6666666666
In VB
answer = .6666666666
我认为这在数字上是不可能的。 1年零11个月你会做什么? 1.11?因为这可能意味着 1 年零 1 个月之类的,或者 1 年零 11 个月。
I think that's numerically impossible. What would you do about 1 year and 11 months? 1.11? Because that could mean either 1 year and 1 month and something, or 1 year and 11 months.