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

前端 未结 4 1512
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 17:40

I\'m working with a protocol that may optionally include a time zone offset when specifying datetime information. My code is written in C# and we are using the 4.0 .NET run

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-06 17:58

    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)
    

提交回复
热议问题