C# program to convert a time to the time in a particular time zone (TimeZoneInfo.ConvertTime() Method)

C# TimeZoneInfo.ConvertTime() Method: Here, we are going to learn how to convert a time to the time in a particular time zone in C#.Net?
Submitted by Nidhi, on May 19, 2021

The ConvertTime() method of TimeZoneInfo class 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

Program:

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 »


ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.