计算年和月中的天数?

发布于 2024-09-26 05:33:46 字数 288 浏览 2 评论 0原文

如何在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 技术交流群。

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

发布评论

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

评论(9

暮凉 2024-10-03 05:33:46

您可以这样做:

double temp  = (234.0 /365.0) * 12.0;

int years = (int)temp;
int months = (int)(temp - years);

这是因为您获得了 0.64,即 0.64 年。如果你想要几个月,你需要乘以 12。

在上面,你会得到 0 年零 7 个月...话虽这么说,我不确定你到底想如何格式化它:

string yearsString = string.Format("{0}.{1}", years, months);

只是请注意,这将在 3.11 中执行 11 个月,这会很奇怪,尽管这是您的要求。

另外,如果您想让它非常通用,您可能需要使用 365.25 而不是 365 来表示单个 儒略年,因为它将帮助您减少因闰年而导致的问题。

You can do:

double temp  = (234.0 /365.0) * 12.0;

int years = (int)temp;
int months = (int)(temp - years);

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:

string yearsString = string.Format("{0}.{1}", years, months);

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.

像你 2024-10-03 05:33:46

如果您不知道实际日期,那么您可以估计:

Number of years: x / 365
Number of months: (x % 365) / 30

其中 % 是模数

If you don't know the actual dates, then you could estimate:

Number of years: x / 365
Number of months: (x % 365) / 30

where % is modulo

春花秋月 2024-10-03 05:33:46

假设一个月正好是一年的十二分之一,并且您想忽略部分月份(根据您的说法,您期望示例中的 7 个月份为 7.688 个月),那么:

int days = 234;
double years = (double)days / 365.242199;
int wholeYears = (int)Math.Floor(years);
double partYears = years - wholeYears;
double approxMonths = partYears * 12;
string horribleFormat = string.Concat(wholeYears, ".", approxMonths);

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:

int days = 234;
double years = (double)days / 365.242199;
int wholeYears = (int)Math.Floor(years);
double partYears = years - wholeYears;
double approxMonths = partYears * 12;
string horribleFormat = string.Concat(wholeYears, ".", approxMonths);
土豪我们做朋友吧 2024-10-03 05:33:46

您确定需要这种格式吗?结果——至少根据当前提供的信息——将是模糊的,因为几个月的长度不一致。

考虑几个替代方案:

  • 仅表示年份,例如 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.

冷…雨湿花 2024-10-03 05:33:46

让我们做一些计算。

1 mon = 0.1
2 mon = 0.2
.
.
9 mon = 0.9
10 mon = 1.0 [WRONG according to you 1 is a year]
fine then 1 / 12 = 0.083, therefore 0.083 is 1 month

Now, 
234 / 365 = 0.64 => 0.64 / 0.083 => 7.7  i.e. 7th month

Therefore fx => days / 365 = ans  % 0.083 = result.

我没有时间证明其他数字,但你可以尝试绕过这个公式。

Let do some calculations.

1 mon = 0.1
2 mon = 0.2
.
.
9 mon = 0.9
10 mon = 1.0 [WRONG according to you 1 is a year]
fine then 1 / 12 = 0.083, therefore 0.083 is 1 month

Now, 
234 / 365 = 0.64 => 0.64 / 0.083 => 7.7  i.e. 7th month

Therefore fx => days / 365 = ans  % 0.083 = result.

I have no time to prove other number but you can try around this formula.

就像说晚安 2024-10-03 05:33:46

我的建议是使用 DateTime.AddDays:它将为您提供所需的一切。您还可以在那里添加其他时间单位:

    DateTime f = new DateTime(0);
    var y = f.AddDays(361);

My suggestion would be to use DateTime.AddDays: it will give you all you need. You can also add other time units there:

    DateTime f = new DateTime(0);
    var y = f.AddDays(361);
像你 2024-10-03 05:33:46

伪代码

ts= TimeSpan.fromDays(385)
Years =  ts.days Modulo 365
months = (ts.days remainder 365) modulo 12
days = (ts.days remainder 365) Remainder 12
answer string years + "." + months + "." + days

Pseudo code

ts= TimeSpan.fromDays(385)
Years =  ts.days Modulo 365
months = (ts.days remainder 365) modulo 12
days = (ts.days remainder 365) Remainder 12
answer string years + "." + months + "." + days
我不咬妳我踢妳 2024-10-03 05:33:46

在 VB 中

    Dim d As DateTime = DateTime.MinValue 'year = 0001
    d = d.AddYears(DateTime.Now.Year - 1) 'add current year
    d = d.AddDays(234) 'add days

    Dim yrs As Integer = d.Year - DateTime.Now.Year 'calculate years
    Dim mos As Double = d.Month / 12 'calculate months
    Dim answer As Double = yrs + mos

答案 = .6666666666

In VB

    Dim d As DateTime = DateTime.MinValue 'year = 0001
    d = d.AddYears(DateTime.Now.Year - 1) 'add current year
    d = d.AddDays(234) 'add days

    Dim yrs As Integer = d.Year - DateTime.Now.Year 'calculate years
    Dim mos As Double = d.Month / 12 'calculate months
    Dim answer As Double = yrs + mos

answer = .6666666666

初雪 2024-10-03 05:33:46

我认为这在数字上是不可能的。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文