根据c#中的天数获取月数

发布于 2024-11-26 21:34:02 字数 260 浏览 0 评论 0原文

我一直在试图解决这个问题。

目前,我们的报告有一个日期标准,该标准受天数限制,当然可以配置,目前设置为 90 天。消息说,它受 90 天限制,但是我的老板想将其增加到 13 个月,不幸的是,如果我这样做,我需要按天完成,它会说,395 天..

这不是一个非常友好的消息..

试图找到一种方法来满足这个要求,我的另一个唯一选择是添加其他受月份和日期限制的设置。但我仍然需要将月份转换回天数,这并不完美,因为并非每个月都有相同的天..

有想法吗?

I'm stuck trying to figure this one out..

We currently have a date criteria on our reports, that are limited by days, configurable of course, currently set to 90 days.. message says, it is limited by 90 days, however my boss wants to increase it to 13 months, unfortunately if I did that, I'd need to do it by days and it would say, 395 days..

Not a very friendly message..

Trying to figure out a way to satisfy this, my other only option is to add another settings that is limited by months as well as days. but then i still need to convert the months back to days which wont be perfect since not every month has same days..

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

凉月流沐 2024-12-03 21:34:03

考虑到天数,我会做这样的事情:

int myDays;   // 390 or whatever
DateTime d1 = DateTime.Now;
DateTime d2 = d1.AddDays(myDays);
int monthsDiff = d2.Month - d1.Month + 12 * (d2.Year - d1.Year);
DateTime d3 = d1.AddMonths(monthsDiff);
TimeSpan tf = d2 - d3;
string msg = monthsDiff.ToString() + " months, " + tf.Days + " days";

I would do something like this, given the number of days:

int myDays;   // 390 or whatever
DateTime d1 = DateTime.Now;
DateTime d2 = d1.AddDays(myDays);
int monthsDiff = d2.Month - d1.Month + 12 * (d2.Year - d1.Year);
DateTime d3 = d1.AddMonths(monthsDiff);
TimeSpan tf = d2 - d3;
string msg = monthsDiff.ToString() + " months, " + tf.Days + " days";
嘦怹 2024-12-03 21:34:03

TimeSpan 为您提供两个 DateTime 对象之间的持续时间。它可以以天、小时或分钟为单位持续给出结果;月数将根据实际开始和结束而有所不同。结束日期,因为不同月份的实际天数不同。

话虽如此,您始终可以编写一个实用程序方法,为您提供 YourTimeSpan 对象,该对象根据您的日历和 StartDate / EndDates 为您提供月数等。

根据您的情况,您可以通过将其单独存储在配置中来使其变得更加简单,例如 - ReportDuration_YearsReportDuration_MonthsReportDuration_Days。这将允许您在报告上创建有意义的标签,并允许正确识别开始日期和结束日期。

//Call this by passing values from configuration
private string GetNiceLookingLable(int? years, int? months, int? days){
    var yearMessage = (years.HasValue)?String.Format("{0} Years", years):String.Empty;
    var monthMessage = (months.HasValue)?String.Format("{0} Months", months):String.Empty;
    var daysMessage = (days.HasValue)?String.Format("{0} Days", days):String.Empty;

    // You probably want to concatenate them properly
    return String.Format("{0} {1} {2}",yearMessage, monthMessage, daysMessage);
}

-

//Call this to get starting date
private DateTime getStartingDate(int? years, int? months,int? days){
     var retDate = DateTime.Today;
     if(years.HasValue){
         retDate = retDate.AddYears(-1*years.Value);
     }
     if(months.HasValue){
         retDate = retDate.AddMonths(-1*months.Value);
     }
     if(days.HasValue){
         retDate = retDate.AddDays(-1*days.Value);
     }

     return retDate;
}

TimeSpan give you duration between two DateTime objects. It can give it consistently in Days, Hours or Mins; Number of months would be different based upon actual start & end dates as different months have different number of actual days.

Having said that, you can always write a Utility method that gives you YourTimeSpan object that gives you number of Months etc based upon your calendar and StartDate / EndDates.

In your case you can make it even simpler by storing it separately in configuration, for example - ReportDuration_Years, ReportDuration_Months, ReportDuration_Days. This would allow you to create meaningful lable on your report as well as allow to identify StartDate and EndDate properly.

//Call this by passing values from configuration
private string GetNiceLookingLable(int? years, int? months, int? days){
    var yearMessage = (years.HasValue)?String.Format("{0} Years", years):String.Empty;
    var monthMessage = (months.HasValue)?String.Format("{0} Months", months):String.Empty;
    var daysMessage = (days.HasValue)?String.Format("{0} Days", days):String.Empty;

    // You probably want to concatenate them properly
    return String.Format("{0} {1} {2}",yearMessage, monthMessage, daysMessage);
}

-

//Call this to get starting date
private DateTime getStartingDate(int? years, int? months,int? days){
     var retDate = DateTime.Today;
     if(years.HasValue){
         retDate = retDate.AddYears(-1*years.Value);
     }
     if(months.HasValue){
         retDate = retDate.AddMonths(-1*months.Value);
     }
     if(days.HasValue){
         retDate = retDate.AddDays(-1*days.Value);
     }

     return retDate;
}
混浊又暗下来 2024-12-03 21:34:02

您需要决定是使用 13 个月作为时间间隔,还是使用近似 13 个月的某个天数。如果您使用 13 个月,那么天数(或报告的结束日期)将根据开始日期而变化。

我建议您将报告配置为几个月或几天(不仅存储数字,还存储配置中的单位)。然后,您可以在报告上显示配置中指定的任何内容(也包括配置中的单位),并通过将配置的已配置单位数添加到开始日期来计算查询的结束日期。

如果你试图在几天内完成所有事情,当你现在需要几个月的时间来工作时,你只会让自己的生活变得困难。

在开始日期上加上 13 个月来获得结束日期比尝试(不准确地)计算出给定天数中有多少个月要容易得多。

You need to decide if you're going to use 13 months as the time interval, or some number of days that approximates to 13 months. If you use 13 months, then the number of days (or the end date for your report) is going to vary depending on the start date.

I would suggest making your report configurable for either months or days (storing not just the number, but the units in configuration). You can then display on the report whatever has been specified in the configuration (with the units from configuration, too) and calculate the end date for the query by adding the configured number of configured units to the start date.

If you try to do everything in days, when you're now working in months, you'll just make life difficult for yourself.

It's much easier to add 13 months to the start date to get the end date, than it is to try and (inaccurately) work out how many months in a given number of days.

破晓 2024-12-03 21:34:02

使用 TimeSpan 对象 执行日期所需的计算标准。

Use the TimeSpan object to perform the calculations you need for your date criteria.

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