Should I store DateTimes as a Long (Ticks) in a database?

后端 未结 4 1924
刺人心
刺人心 2021-01-12 11:00

Could life be made easier by saving DateTime values as a long instead? There always seem to be problems when working with null DateTime values, whether storing

4条回答
  •  北海茫月
    2021-01-12 11:32

    I can't think of any outstanding reason personally, databases support DateTime's themselves, and storing them as a long you may end up shooting yourself in the foot. Let's say you need to be able to run a query "Get me all rows between 3 AM and 6 PM" - if you store them as ticks, you will need to convert back to a DateTime in the database.

    Storing them as ticks may impede many other operations, such as grouping, sorting, filtering, etc.

    If you have nuances with DateTimes in the database, such as TimeZone, it's strongly recommended to normalize the DateTime to a specific timezone, such as UTC. A lot of the problems that teams face with DateTime in the database is often due to unsanitary input, like not normalizing the TimeZone. Storing it as ticks will still have the same problem.

提交回复
热议问题