I want to serialize the Date portion of a DateTime into 4 bytes (could be a Int32). What is the fastest way to do so?
Background: in order to serialize a full DateTime I have been using the ToBinary method so far. It returns a Int64 that I'm storing elsewhere. Now, I have the requirement to store only the Date part of the DateTime using only half the space. So, I was wondering how to achieve this in the fastest way as performance is crucial.
Options that come into my mind is:
- Encode Year, Month, Day into and
intas YYYYMMDD by using some multiplications and property accesses with the nice side-effect that this encoding is human-readable. - Keep using
ToBinaryand keep only "the upper or lower half" of the returnedlong. Don't know if that is possible. - Check how
DateTimeare stored internally. Maybe the date portion can be accessed in other ways.
How would you do it?