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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you’re using .Net 4.0 or above, you can use the
ToStringmethod on timespan with thehhandmmspecifier (not sure if it will display the + and – signs though):If not, you can just format the
HoursandMinutesproperties 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