它还应该考虑夏令时。请忽略上述问题,因为它很快就会被删除。
我的应用程序有一个从 utc 转换为 bst 的任务。
using System;
namespace DisplayBSTTime
{
class Program
{
static void Main()
{
// Get the UTC time
DateTime utcTime = DateTime.UtcNow;
// Find the timezone information for GMT Standard Time (handles BST automatically)
TimeZoneInfo londonTimeZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
// Convert UTC time to London time (BST or GMT based on the date)
DateTime bstTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, londonTimeZone);
// Display the time in BST
Console.WriteLine("BST Time: " + bstTime.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
}
private void DisplayBSTTime()
{
DateTime utcTime = DateTime.UtcNow;
TimeZoneInfo londonTimeZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
DateTime bstTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, londonTimeZone);
// Assuming you have a label to display the time
lblTime.Text = bstTime.ToString("yyyy-MM-dd HH:mm:ss");
}