DateTime RoundDown(DateTime date, TimeSpan interval)
{
return new DateTime(date.Ticks / interval.Ticks *
interval.Ticks);
}
using it
DateTime currentTime = RoundDown(DateTime.Now, TimeSpan.FromMinutes(-5));
but i want to add an option that it will check for the current pc time and if for example the time is 22:51 then round down to 22:50 meaning round down by 1.
so something like if i will call the method RoundDown like : RoundDown(); it will automatic check if to round down by 1 depending on the pc time.
if i will call it by : RoundDown(DateTime.Now, TimeSpan.FromMinutes(-5)); then it will round down by 5 or any number i give it.
CodePudding user response:
That would just be:
public static void Main (string[] args) {
Console.WriteLine(RoundDown(DateTime.Now, 5));
}
public static DateTime RoundDown(DateTime dt, int NearestMinuteInterval)
{
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute / NearestMinuteInterval * NearestMinuteInterval, 0);
}