C# DateTime: What “date” to use when I'm using just the “time”?

后端 未结 12 1617
陌清茗
陌清茗 2020-12-16 09:35

I\'m using a DateTime in C# to display times. What date portion does everyone use when constructing a time?

E.g. the following is not valid because ther

12条回答
  •  一向
    一向 (楼主)
    2020-12-16 10:24

    Personally I'd create a custom Time struct that contains a DateTime instance, and which has similar properties, constructors etc. but doesn't expose days/months/etc. Just make all your public accessors pass through to the contained instance. Then you can simply have the epoch as a private static readonly DateTime field and it doesn't matter what value you choose as it's all neatly contained within your custom struct. In the rest of your code can simply write:

    var time = new Time(16, 47, 58);
    

提交回复
热议问题