Is there a simple function for rounding a DateTime down to the nearest 30 minutes, in C#?

后端 未结 7 1050
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 01:35

For example:

2011-08-11 16:59 becomes 2011-08-11 16:30

相关标签:
7条回答
  • 2020-12-02 02:35

    There is no such function available

    You can refer to : How can I round up the time to the nearest X minutes?

    IF needed you can create one yourself

    DateTime RoundUp(DateTime dt, TimeSpan d)
    {
       return new DateTime(((dt.Ticks - d.Ticks + 1) / d.Ticks) * d.Ticks + d.Ticks);
    }
    

    eg:

    var dt1 = RoundUp(DateTime.Parse("2011-08-11 16:59"), TimeSpan.FromMinutes(30));
    // dt1 == {11/08/2011 16:30:00}
    
    0 讨论(0)
提交回复
热议问题