Home »
.Net »
C# Programs
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 »