C# - TimeZoneInfo.GetAmbiguousTimeOffsets() Method with Example

In this tutorial, we will learn about the C# TimeZoneInfo.GetAmbiguousTimeOffsets() method with its definition, usage, syntax, and example. Submitted by Nidhi Last updated : March 31, 2023

TimeZoneInfo.GetAmbiguousTimeOffsets() Method

The TimeZoneInfo.GetAmbiguousTimeOffsets() is used to get the information about the possible dates & times that an ambiguous date & time can be mapped to.

Syntax

TimeSpan [] TimeZoneInfo.GetAmbiguousTimeOffsets(DateTime ambiguousTime);

Parameter(s)

  • ambiguousTime: A date and time.

Return Value

This method returns an array of TimeSpan that represent ambiguous offsets.

Exception(s)

  • System.ArgumentException

C# Example of TimeZoneInfo.GetAmbiguousTimeOffsets() Method

The source code to get the ambiguous time offsets is given below. The given program is compiled and executed successfully.

using System;
using System.Globalization;
using System.Collections.ObjectModel;

class TimeZoneInfoDemo {
  //Entry point of Program
  static public void Main() {
    DateTime ambiguousTime;

    TimeZoneInfo timeZone1 = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    TimeZoneInfo timeZone2 = TimeZoneInfo.FindSystemTimeZoneById("UTC");

    ambiguousTime = new DateTime(2007, 11, 4, 1, 0, 0);

    if (timeZone1.IsAmbiguousTime(ambiguousTime)) {
      Console.WriteLine("Time is ambiguous");

      TimeSpan[] ts = timeZone1.GetAmbiguousTimeOffsets(ambiguousTime);
      Console.WriteLine("Ambiguous time offsets: ");
      foreach(TimeSpan t in ts) {
        Console.WriteLine("\t" + t);
      }
    } else
      Console.WriteLine("Time is not ambiguous");

    if (timeZone2.IsAmbiguousTime(ambiguousTime)) {
      Console.WriteLine("Time is ambiguous");

      TimeSpan[] ts = timeZone2.GetAmbiguousTimeOffsets(ambiguousTime);
      Console.WriteLine("Ambiguous time offsets: ");
      foreach(TimeSpan t in ts) {
        Console.WriteLine("\t" + t);
      }
    } else
      Console.WriteLine("Time is not ambiguous");
  }
}

Output

Time is ambiguous
Ambiguous time offsets:
        -06:00:00
        -05:00:00
Time is not ambiguous
Press any key to continue . . .

C# TimeZoneInfo Class Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.