Have you tried something like the following? I have successfully used a similar method to round up to the next hour, minute, day etc...
private static readonly long _ticksIn30Mins = TimeSpan.FromMinutes(30).Ticks;
protected DateTime GetRoundedTime(DateTime inputTime)
{
long currentTicks = inputTime.Ticks;
return new DateTime(currentTicks.RoundUp(_ticksIn30Mins));
}
public static class ExtensionMethods
{
public static long RoundUp(this long i, long toTicks)
{
return (long)(Math.Round(i / (double)toTicks,
MidpointRounding.AwayFromZero)) * toTicks;
}
}
This takes the RoundOff method from this previous question. You just need to modify it to always round up by using MidpointRoundingMode.AwayFromZero.
Finally to cope with the specific case of 12:00am becoming 12:30am then check if your before rounding and after rounding value are the same, and if so, increment the roundup amount of ticks (e.g. 30mins)
var currentTime = DateTime.Now;
var rounded = GetRoundedTime(currentTime);
if (rounded == currentTime)
{
rounded = new DateTime(rounded.Ticks + _ticksIn30Mins);
}
For a tested console application that demonstrates this principle, please see the below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
private static readonly long _ticksIn30Mins = TimeSpan.FromMinutes(30).Ticks;
static void Main(string[] args)
{
WriteDateString(new DateTime(2012, 01, 18, 09, 45, 11, 152));
WriteDateString(new DateTime(2012, 01, 18, 12, 15, 11, 999));
WriteDateString(new DateTime(2012, 01, 18, 12, 00, 00, 000));
Console.ReadLine();
}
private static void WriteDateString(DateTime dateTime)
{
Console.WriteLine("Before: {0}, After: {1}", dateTime, GetRoundedTime(dateTime));
}
private static DateTime GetRoundedTime(DateTime inputTime)
{
long currentTicks = inputTime.Ticks;
var rounded = new DateTime(currentTicks.RoundUp(_ticksIn30Mins));
if (rounded == inputTime)
{
rounded = new DateTime(rounded.Ticks + _ticksIn30Mins);
}
return rounded;
}
}
public static class ExtensionMethods
{
public static long RoundUp(this long i, long toTicks)
{
return (long)(Math.Round(i / (double)toTicks, MidpointRounding.AwayFromZero)) * toTicks;
}
}
}
Output:

Best regards,