How can I format a TimeSpan
object to look like a time zone offset, like this:
+0700
or
-0600
I'm using GetUtcOffset to get an offset, and its working, but its returning a TimeSpan object.
How can I format a TimeSpan
object to look like a time zone offset, like this:
+0700
or
-0600
I'm using GetUtcOffset to get an offset, and its working, but its returning a TimeSpan object.
I think you could use this:
Try something like:
I add the + when the number is non-negative (for negative numbers a - should be output).
If you're using .Net 4.0 or above, you can use the
ToString
method on timespan with thehh
andmm
specifier (not sure if it will display the + and - signs though):If not, you can just format the
Hours
andMinutes
properties along with some conditional formatting to always display the + and - signs:Reference for TimeSpan format strings: http://msdn.microsoft.com/en-gb/library/ee372287.aspx
Reference for numeric format strings and conditional formatting of them: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx