Custom Formatter with String.Format

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

    Custom Formatter with String.Format

    TimeSpan utcOffset = localZone.GetUt cOffset(DateTim e.Now);
    MessageLabel.Te xt = utcOffset.ToStr ing( ); // returns -06:00:00

    Instead of -06:00:00 I want -06:00.
    Apparently the custom formatter should be 0:zzz but I can't figure out the
    correct String.Format grammar to get the desired result.
    Can you?

    <%= Clinton Gallagher


  • Tim Scott

    #2
    Re: Custom Formatter with String.Format

    Clinton,

    I did some searching for "TimeSpan format" on the .NET newsgroups, and
    found some other messages asking similar questions. It looks like
    TimeSpan is not as nicely formattable as other classes and structs in
    ..NET (like DateTime). One suggestion to get what you were asking for
    is something like:

    string formatedTimeSpa n=String.Format ("{0:00}:{1:00} ", utcOffset.Hours ,
    utcOffset.Minut es);

    See


    -- Tim Scott


    Comment

    • Dale

      #3
      RE: Custom Formatter with String.Format

      If you're just formatting your time for display you could do something like

      DateTime displayTime = DateTime.MinVal ue + time;
      Console.WriteLi ne(displayTime. ToString("-HH:mm");

      HTH
      --
      Dale Preston
      MCAD C#
      MCSE, MCDBA


      "clintonG" wrote:
      [color=blue]
      > TimeSpan utcOffset = localZone.GetUt cOffset(DateTim e.Now);
      > MessageLabel.Te xt = utcOffset.ToStr ing( ); // returns -06:00:00
      >
      > Instead of -06:00:00 I want -06:00.
      > Apparently the custom formatter should be 0:zzz but I can't figure out the
      > correct String.Format grammar to get the desired result.
      > Can you?
      >
      > <%= Clinton Gallagher
      >
      >
      >[/color]

      Comment

      • clintonG

        #4
        Re: Custom Formatter with String.Format

        Thanks Tim, that works just fine.

        <%= Clinton Gallagher

        "Tim Scott" <timscott@gmail .com> wrote in message
        news:1138425761 .916936.229760@ o13g2000cwo.goo glegroups.com.. .[color=blue]
        > Clinton,
        >
        > I did some searching for "TimeSpan format" on the .NET newsgroups, and
        > found some other messages asking similar questions. It looks like
        > TimeSpan is not as nicely formattable as other classes and structs in
        > .NET (like DateTime). One suggestion to get what you were asking for
        > is something like:
        >
        > string formatedTimeSpa n=String.Format ("{0:00}:{1:00} ", utcOffset.Hours ,
        > utcOffset.Minut es);
        >
        > See
        > http://groups.google.com/group/micro...a8dcf28516873f
        >
        > -- Tim Scott
        > http://geekswithblogs.net/tscott
        >[/color]


        Comment

        • clintonG

          #5
          Re: Custom Formatter with String.Format

          Thank you for that suggestion Dale.

          <%= Clinton Gallagher


          "Dale" <dale0973@nospa m.nospam> wrote in message
          news:87CC4D56-87F4-4C8A-B4AB-021F9670D9ED@mi crosoft.com...[color=blue]
          > If you're just formatting your time for display you could do something
          > like
          >
          > DateTime displayTime = DateTime.MinVal ue + time;
          > Console.WriteLi ne(displayTime. ToString("-HH:mm");
          >
          > HTH
          > --
          > Dale Preston
          > MCAD C#
          > MCSE, MCDBA
          >
          >
          > "clintonG" wrote:
          >[color=green]
          >> TimeSpan utcOffset = localZone.GetUt cOffset(DateTim e.Now);
          >> MessageLabel.Te xt = utcOffset.ToStr ing( ); // returns -06:00:00
          >>
          >> Instead of -06:00:00 I want -06:00.
          >> Apparently the custom formatter should be 0:zzz but I can't figure out
          >> the
          >> correct String.Format grammar to get the desired result.
          >> Can you?
          >>
          >> <%= Clinton Gallagher
          >>
          >>
          >>[/color][/color]


          Comment

          Working...