473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 5478
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Eric Workman | last post by:
Hi Everyone- I would like to implement the IFormatter class to create a custom formatter that essentially does exactly what the System.Xml.Serialization.XmlSerializer does, but also would allow...
7
by: Roger Helliwell | last post by:
Hello Everyone, Has anyone found a quick reference for the many string formatting options for string.Format() ? I'm spending a ridiculous amount of time trying to write one line of code that...
2
by: david.kao | last post by:
Hi All: I need an easy way to format a string. Here is my situation: when the user inputs a character 'A', my logic suppose adding two ##. The expecting result is ##A. when the user inputs two...
2
by: Nadav | last post by:
Hi, I am trying to create a custom formatter ( such as the binary formatter ), I can't figure out where do I bind the input Data Stream with the SerializationInfo object info how should i create...
5
by: Burak | last post by:
Hello, I would like to format the string "11304200" into "11-3042.00". Can I do this with String.Format method? I have not come across any good documentation. Thank you,
0
by: kplkumar | last post by:
In Enterprise Library 2.0, I created a custom formatter as below, public class ERFormatter : LogFormatter { ...... // ________________________________________________________________ The...
1
by: Alan Foxmore | last post by:
Hello all, Is it possible to use String.Format() to specify a maximum length for a formatted item? For example, let's say I have: String.Format("{0}", "FREDDY"); How can I specify that the...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
8
by: Armando Rocha | last post by:
Hi, Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in form like this "25DD-68ED-EB8D-5E11", i try String.Format("{0:####-####-####-####}", mystr), but not work, i think...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.