如何在 C Sharp 应用程序(winforms)上从 UTC 转换为英国夏令时间?

问题描述 投票:0回答:1

它还应该考虑夏令时。请忽略上述问题,因为它很快就会被删除。

我的应用程序有一个从 utc 转换为 bst 的任务。

c#
1个回答
0
投票
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");
}
© www.soinside.com 2019 - 2024. All rights reserved.