How to control appearance of ':' in time zone offset when parsing/formatting Datetime

Doesn't look like there is anything built-in (you can use zz, but that leaves out the minutes).

You can roll your own by instantiating a DateTimeFormatInfo, setting TimeSeparator to string.Empty and using that as the IFormatProvider when calling DateTime.ToString (and make the call explicit, if it is not already).

But frankly, using Replace to remove the unwanted : from the default return value is so much easier.


I faced the same problem, ended up using an extension

    public static class DateTimeExtensions
    {        
        public static String ToSomeFormat(this DateTimeOffset dateTime)
        {
            return dateTime.ToString("yyyyMMddHHmmsszzz").Replace(":", "");
        }
    }

If you're using it in a place where it doesn't make sense to use replace or extend (for example something that might want to output as -05:00 with a colon when passed as zzz) and the minutes don't matter you could fake it with zz00.

var date = new DateTimeOffset(2008, 8, 1, 0, 0, 0, new TimeSpan(-5, 0, 0));
Console.WriteLine(date.ToString("yyyy-MM-dd-HH:mm:ss(zz00)"));
// outputs 2008-08-01-00:00:00(-0500)