Magic strings for converting DateTime to string Using C#

前端 未结 4 1060

I was greeted with a nasty bug today. The task is pretty trivial, all I needed to do is to convert the DateTime object to string in \"yyyymmdd\" format

4条回答
  •  [愿得一人]
    2021-01-27 15:25

    If that format string bugs you that much, at least make sure it is in one place. Encapsulate it e.g. in an extension method:

    public string ToMyAppsPrefferedFormat(this DateTime date) {
      return date.ToString("ddMMyyyy");
    }
    

    Then you can say date.ToMyAppsPrefferedFormat()

提交回复
热议问题