Storing Time in System.DateTime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert

    #1

    Storing Time in System.DateTime

    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
  • Jon Skeet [C# MVP]

    #2
    Re: Storing Time in System.DateTime

    Robert <cheetham@pobox .upenn.edu> wrote:[color=blue]
    > 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?[/color]

    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.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Bruno Jouhier [MVP]

      #3
      Re: Storing Time in System.DateTime

      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.

      "Robert" <cheetham@pobox .upenn.edu> a écrit dans le message de
      news:06d101c3e9 43$a91d3490$350 1280a@phx.gbl.. .[color=blue]
      > 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[/color]


      Comment

      Working...