如何在 C# 中将特定时区的时间转换为本地系统时区?

How to convert time of a specific zone to local system time zone in C#?

我从列记录的格式为“03:45:00 AM CST”的文件中获得了时间格式。此外,我必须将其转换为 utc,然后转换为 est 时区或本地系统时间。到目前为止,我已经尝试过这种方法,但都是一脉相承的。任何帮助将不胜感激。

//Converting Time in CST to UTC 
f= "03:45:00 AM CST"
var fetchtime = f.Substring(0,12);
var converttimetostring = Convert.ToDateTime(fetchtime).ToString();
var timeconvert = DateTime.ParseExact(converttimetostring,"HH:mm:ss",CultureInfo.InvariantCulture);

DateTime timeUtc = DateTime.UtcNow;
TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime cstTime = TimeZoneInfo.ConvertTimeToUtc(timeconvert,cst);
Console.WriteLine(cstTime);

//Converting time FROM UTC to EST(East. Standard Time)
DateTime localtime = cstTime.ToLocalTime();
Console.WriteLine(localtime);

我使用以下代码片段将输出设为“04:45:00 AM”——cst 和 est 之间相差一小时。

//Converting from cst to UTC
f="03:45:00 AM CST";
var fetchtime = f.Substring(0,12);
DateTime timeUtc = DateTime.UtcNow;
TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var a = DateTime.Parse(fetchtime);
DateTime cstTime = TimeZoneInfo.ConvertTimeToUtc(a,cst);
Console.WriteLine(cstTime);

//Converting time FROM UTC to EST(East. Standard Time)
DateTime localtime = cstTime.ToLocalTime();
Console.WriteLine(localtime);