DOTNET的舍入日期时间到最后15分钟 [英] DotNet Roundoff datetime to last 15 minutes

查看:117
本文介绍了DOTNET的舍入日期时间到最后15分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个函数四舍五入日期时间到最后一个季度?

Is there a function to round off the datetime to last quarter?

示例...

08:03:00 becomes 08:00:00

08:14:00 becomes 08:00:00

08:15:00 stays   08:15:00

08:16:00 becomes 08:15:00

08:29:00 becomes 08:15:00

08:45:00 stays   08:45:00 

08:55:00 becomes 08:45:00

09:01:00 becomes 09:00:00

我已经写了下面的函数,但它返回下一季度

private DateTime RoundUpToPreviousQuarter(DateTime date, TimeSpan d)
{
      return new DateTime(((date.Ticks + d.Ticks - 1) / d.Ticks) * d.Ticks);
}

// call the method
this.RoundUp(time, TimeSpan.FromMinutes(15));

任何输入AP preciated。

Any inputs appreciated.

推荐答案

应用模15日期时间的分钟财产,减去相同的属性值

Apply the modulo 15 to the datetime Minute property and subtract that value from the same property

DateTime dt = new DateTime(2013,5,28, 15, 59,0);
dt = dt.AddMinutes(-(dt.Minute % 15));

在这个例子中,我创建了一个零秒的日期时间。如果您还需要去除秒

In this example I have created a zero seconds datetime. If you need to remove also the seconds

DateTime dt = new DateTime(2013,5,28, 15, 59,45);
dt = dt.AddMinutes(-(dt.Minute % 15)).AddSeconds(-dt.Second);

这篇关于DOTNET的舍入日期时间到最后15分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆