Datetime convert

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

    #1

    Datetime convert

    Hi,

    can someone explain me, when to use:
    (DateTime)DataB inder.Eval(Cont ainer.DataItem, "dateField" )

    OR

    Convert.ToDateT ime(DataBinder. Eval(Container. DataItem, "dateField" ))

    Sometimes (DateTime) works sometimes not, on the other hand
    Convert.ToDateT ime() always works.

    The same problem is with (int16) and Convert.ToInt16

    Thanks,S




  • Jon Skeet [C# MVP]

    #2
    Re: Datetime convert

    SimonZ <simon.zupan@st udio-moderna.com> wrote:[color=blue]
    > can someone explain me, when to use:
    > (DateTime)DataB inder.Eval(Cont ainer.DataItem, "dateField" )
    >
    > OR
    >
    > Convert.ToDateT ime(DataBinder. Eval(Container. DataItem, "dateField" ))
    >
    > Sometimes (DateTime) works sometimes not, on the other hand
    > Convert.ToDateT ime() always works.
    >
    > The same problem is with (int16) and Convert.ToInt16[/color]

    When you cast, the runtime type of the object must be exactly correct
    (leaving conversion operators aside for the moment as they're not
    relevant in your example).

    When you use Convert.ToDateT ime(object), the object just has to be of
    some type which implements IConvertible and does the appropriate thing
    with IConvertible.To DateTime.

    In other words, it depends what DataBinder.Eval actually returns. If it
    returns a string, for instance, Convert.ToDateT ime will work (because
    System.String implements IConvertible and tries to parse the string as
    a date/time) but casting won't.

    Does that help?

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: Datetime convert

      Hi,


      An addition to the OP comments, Convert.ToDateT ime does not always work,
      there are a number (most of them) that raise exception , IIRC only when
      received a string it does the conversion.



      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation


      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.1e147f 4f60687f9e98cb9 8@msnews.micros oft.com...[color=blue]
      > SimonZ <simon.zupan@st udio-moderna.com> wrote:[color=green]
      >> can someone explain me, when to use:
      >> (DateTime)DataB inder.Eval(Cont ainer.DataItem, "dateField" )
      >>
      >> OR
      >>
      >> Convert.ToDateT ime(DataBinder. Eval(Container. DataItem, "dateField" ))
      >>
      >> Sometimes (DateTime) works sometimes not, on the other hand
      >> Convert.ToDateT ime() always works.
      >>
      >> The same problem is with (int16) and Convert.ToInt16[/color]
      >
      > When you cast, the runtime type of the object must be exactly correct
      > (leaving conversion operators aside for the moment as they're not
      > relevant in your example).
      >
      > When you use Convert.ToDateT ime(object), the object just has to be of
      > some type which implements IConvertible and does the appropriate thing
      > with IConvertible.To DateTime.
      >
      > In other words, it depends what DataBinder.Eval actually returns. If it
      > returns a string, for instance, Convert.ToDateT ime will work (because
      > System.String implements IConvertible and tries to parse the string as
      > a date/time) but casting won't.
      >
      > Does that help?
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      Working...