CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: timezone offset

  1. #1
    Join Date
    Apr 2005
    Posts
    27

    timezone offset

    hello there folks and how are we all.

    seems like a great forum and clever bunch of people here also.

    just recently started playing around with c# and really like it hope to get fairly good at it.

    i have the following code which works and cant help wondering if theres a better easier way to do it or is what i have done exceptable?

    Code:
    string UTCOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).ToString();
    
    UTCOffset = UTCOffset.Replace(":", "").Substring(0, 4);
    
    Console.WriteLine("UTC offset: " + UTCOffset);
    comments appreciated, many thanks.

  2. #2
    Join Date
    Apr 2005
    Location
    Melbourne, Australia
    Posts
    14

    Re: timezone offset

    Try this:
    TimeSpan UTCOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
    Console.WriteLine("UTC offset: {0}{1}",
    UTCOffset.Hours.ToString("00"),
    UTCOffset.Minutes.ToString("00"));

    This way you are directly working with the timespan object and not formatting strings unnecessarily.

  3. #3
    Join Date
    Apr 2005
    Posts
    27

    Re: timezone offset

    thanks pete very nice indeed didnt even know you could do that

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured