C# - TimeZoneInfo.ConvertTime() Method with Example

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

TimeZoneInfo.ConvertTime() Method

The TimeZoneInfo.ConvertTime() is a static method that it used to convert a time to the time in a particular time zone.

Syntax

DateTime TimeZoneInfo.ConvertTime(DateTime date-time, TimeZoneInfo destTimeZone);

Parameter(s)

  • date-time: date-time to be converted in a particular time zone.
  • destTimeZone: The time zone to convert dateTime to.

Return Value

This method returns the date and time after conversion in a particular time zone.

Exception(s)

  • System.ArgumentException
  • System.ArgumentNullException

C# Example of TimeZoneInfo.ConvertTime() Method

The source code to convert a time to the time in a particular time zone 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() {
    TimeZoneInfo easternStandardTime;
    DateTime time;
    DateTime convertedTime;

    time = new DateTime(2015, 2, 1, 0, 21, 20, DateTimeKind.Utc);
    easternStandardTime = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

    convertedTime = TimeZoneInfo.ConvertTime(time, easternStandardTime);

    Console.WriteLine("Time before conversion: " + time);
    Console.WriteLine("Time After conversion:  " + convertedTime);
  }
}

Output

Time before conversion: 2/1/2015 12:21:20 AM
Time After conversion:  1/31/2015 7:21:20 PM
Press any key to continue . . .

C# TimeZoneInfo Class Programs »





Comments and Discussions!

Load comments ↻





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