从日期时间生成相对日期范围(本周、今年、上个月等)的最佳方法是什么?

发布于 2024-07-16 02:47:35 字数 373 浏览 4 评论 0原文

我确信我不是第一个需要这样做的人,所以我正在寻找最好的方法。

我有一组单选按钮,其中包含诸如今年去年本月上个月本周上周等选项

  • 需要
  • 日期
  • 当前

生成正确的相对日期范围(DateTime.Now )。

例如,如果选择了去年并且当前日期为4/2/09 14:45:32,我需要返回开始日期1/ 1/08 00:00:00 和结束日期 12/31/08 23:59:59

有什么想法吗?

I'm sure I'm not the first person to need to do this, so I'm looking for the best way.

I've got a set of radio buttons with choices such as

  • This Year
  • Last Year
  • This Month
  • Last Month
  • This Week
  • Last Week

and I need to produce the proper relative date range from the current date (DateTime.Now).

For example if Last Year was selected and the current date was 4/2/09 14:45:32 I would need to return a start date of 1/1/08 00:00:00 and end date of 12/31/08 23:59:59.

Any thoughts?

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

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

发布评论

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

评论(4

独夜无伴 2024-07-23 02:47:35

所有这些都已使用 DateTime.Today 进行了测试,并且按照您的要求工作:

public struct DateRange
{
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
}

public static DateRange ThisYear(DateTime date)
{
    DateRange range = new DateRange();

    range.Start = new DateTime(date.Year, 1, 1);
    range.End = range.Start.AddYears(1).AddSeconds(-1);

    return range;
}

public static DateRange LastYear(DateTime date)
{
    DateRange range = new DateRange();

    range.Start = new DateTime(date.Year - 1, 1, 1);
    range.End = range.Start.AddYears(1).AddSeconds(-1);

    return range;
}

public static DateRange ThisMonth(DateTime date)
{
    DateRange range = new DateRange();

    range.Start = new DateTime(date.Year, date.Month, 1);
    range.End = range.Start.AddMonths(1).AddSeconds(-1);

    return range;
}

public static DateRange LastMonth(DateTime date)
{
    DateRange range = new DateRange();

    range.Start = (new DateTime(date.Year, date.Month, 1)).AddMonths(-1);
    range.End = range.Start.AddMonths(1).AddSeconds(-1);

    return range;
}

public static DateRange ThisWeek(DateTime date)
{
    DateRange range = new DateRange();

    range.Start = date.Date.AddDays(-(int)date.DayOfWeek);
    range.End = range.Start.AddDays(7).AddSeconds(-1);

    return range;
}

public static DateRange LastWeek(DateTime date)
{
    DateRange range = ThisWeek(date);

    range.Start = range.Start.AddDays(-7);
    range.End = range.End.AddDays(-7);

    return range;
}

All of these have been tested using DateTime.Today, and work just like you asked for:

public struct DateRange
{
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
}

public static DateRange ThisYear(DateTime date)
{
    DateRange range = new DateRange();

    range.Start = new DateTime(date.Year, 1, 1);
    range.End = range.Start.AddYears(1).AddSeconds(-1);

    return range;
}

public static DateRange LastYear(DateTime date)
{
    DateRange range = new DateRange();

    range.Start = new DateTime(date.Year - 1, 1, 1);
    range.End = range.Start.AddYears(1).AddSeconds(-1);

    return range;
}

public static DateRange ThisMonth(DateTime date)
{
    DateRange range = new DateRange();

    range.Start = new DateTime(date.Year, date.Month, 1);
    range.End = range.Start.AddMonths(1).AddSeconds(-1);

    return range;
}

public static DateRange LastMonth(DateTime date)
{
    DateRange range = new DateRange();

    range.Start = (new DateTime(date.Year, date.Month, 1)).AddMonths(-1);
    range.End = range.Start.AddMonths(1).AddSeconds(-1);

    return range;
}

public static DateRange ThisWeek(DateTime date)
{
    DateRange range = new DateRange();

    range.Start = date.Date.AddDays(-(int)date.DayOfWeek);
    range.End = range.Start.AddDays(7).AddSeconds(-1);

    return range;
}

public static DateRange LastWeek(DateTime date)
{
    DateRange range = ThisWeek(date);

    range.Start = range.Start.AddDays(-7);
    range.End = range.End.AddDays(-7);

    return range;
}
生活了然无味 2024-07-23 02:47:35

今年:

DateTime Today = DateTime.Today;
DateTime StartDate = new DateTime(Today.Year,1,1);
DateTime EndDate = StartDate.AddYears(1).AddSeconds(-1);

本月: 本周

DateTime Today = DateTime.Today;
DateTime StartDate = new DateTime(Today.Year,Today.Month,1);
DateTime EndDate = StartDate.AddMonths(1).AddSeconds(-1);

DateTime Today = DateTime.Today;
DateTime StartDate = Today.AddDays(-((int) Today.DayOfWeek));
DateTime EndDate = StartDate.AddDays(7).AddSeconds(-1);

去年/月/周是上述的简单变化。 编辑: 本周假设本周从周日开始。 如果您的星期从星期一开始,您将需要稍微修改代码。

This year:

DateTime Today = DateTime.Today;
DateTime StartDate = new DateTime(Today.Year,1,1);
DateTime EndDate = StartDate.AddYears(1).AddSeconds(-1);

This month:

DateTime Today = DateTime.Today;
DateTime StartDate = new DateTime(Today.Year,Today.Month,1);
DateTime EndDate = StartDate.AddMonths(1).AddSeconds(-1);

This week:

DateTime Today = DateTime.Today;
DateTime StartDate = Today.AddDays(-((int) Today.DayOfWeek));
DateTime EndDate = StartDate.AddDays(7).AddSeconds(-1);

Last year / month / week are simple variations on above. Edit: This week assumes the week starts on Sunday. You would have to modify the code slightly if your weeks start on Monday.

温柔嚣张 2024-07-23 02:47:35

我将创建一个工厂方法,该方法将返回一个接口(或您可以执行的委托),该接口将传递当前日期并根据实现返回日期范围。

当然,您从工厂方法返回的实现将由您传递给它的枚举值决定,该枚举对应于“今年”、“去年”等。

I would create a factory method which will return an interface (or a delegate which you can execute) which would be passed the current date and return the date range for you based on the implementation.

Of course, which implementation you return from the factory method would be determined by the value of an enumeration you pass to it which corresponds to "this year", "last year", etc, etc.

拥醉 2024-07-23 02:47:35

我将使用 DateTime 内置方法添加和返回日期的特定部分来编写一个返回间隔的函数。

I would use the DateTime built-in methods for Adding and returning specific portions of the date to write a function that would return the interval.

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