Storing Time in System.DateTime

  • Thread starter Thread starter Robert
  • Start date Start date
R

Robert

I have no problem storing dates + times in a
System.DateTime object. In addition, it's easy to output
a Time as a string from an existing Date/Time. But I'm
having trouble storing a time only. Is there any way to
store only a time without the date portion (like 3:00 am)
in this data structure? Or, if not, is there another
data structure that would be preferable?

Thanks

robert
 
Robert said:
I have no problem storing dates + times in a
System.DateTime object. In addition, it's easy to output
a Time as a string from an existing Date/Time. But I'm
having trouble storing a time only. Is there any way to
store only a time without the date portion (like 3:00 am)
in this data structure? Or, if not, is there another
data structure that would be preferable?

No, it will *always* store the date part as well - but that doesn't
mean that you have to take any notice of it. Just create a new DateTime
which has (say) year 1970, month 1, day 1 and then the right time.
 
You can store it into a TimeSpan, as the interval of time that elapsed
between midnight and your time.

This way you can easily reconstruct a full date time from a date (at
midnight) and a time, you can just add the time expressed as TimeSpan to the
date.

I find this representation cleaner than a DateTime at an arbitrary date (Jan
1st 1970, Jan 1st 1900, ...).

Bruno.
 
Back
Top