471,319 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Custom Formatter with String.Format

TimeSpan utcOffset = localZone.GetUtcOffset(DateTime.Now);
MessageLabel.Text = utcOffset.ToString( ); // 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
Jan 28 '06 #1
4 5369
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 formatedTimeSpan=String.Format("{0:00}:{1:00}", utcOffset.Hours,
utcOffset.Minutes);

See
http://groups.google.com/group/micro...a8dcf28516873f

-- Tim Scott
http://geekswithblogs.net/tscott

Jan 28 '06 #2
If you're just formatting your time for display you could do something like

DateTime displayTime = DateTime.MinValue + time;
Console.WriteLine(displayTime.ToString("-HH:mm");

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"clintonG" wrote:
TimeSpan utcOffset = localZone.GetUtcOffset(DateTime.Now);
MessageLabel.Text = utcOffset.ToString( ); // 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

Jan 29 '06 #3
Thanks Tim, that works just fine.

<%= Clinton Gallagher

"Tim Scott" <ti******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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 formatedTimeSpan=String.Format("{0:00}:{1:00}", utcOffset.Hours,
utcOffset.Minutes);

See
http://groups.google.com/group/micro...a8dcf28516873f

-- Tim Scott
http://geekswithblogs.net/tscott

Jan 29 '06 #4
Thank you for that suggestion Dale.

<%= Clinton Gallagher
"Dale" <da******@nospam.nospam> wrote in message
news:87**********************************@microsof t.com...
If you're just formatting your time for display you could do something
like

DateTime displayTime = DateTime.MinValue + time;
Console.WriteLine(displayTime.ToString("-HH:mm");

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"clintonG" wrote:
TimeSpan utcOffset = localZone.GetUtcOffset(DateTime.Now);
MessageLabel.Text = utcOffset.ToString( ); // 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

Jan 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Eric Workman | last post: by
7 posts views Thread by Roger Helliwell | last post: by
2 posts views Thread by david.kao | last post: by
2 posts views Thread by Nadav | last post: by
reply views Thread by kplkumar | last post: by
1 post views Thread by Alan Foxmore | last post: by
8 posts views Thread by Lucky | last post: by
8 posts views Thread by Armando Rocha | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.