C# 从 Windows 设置中获取 ISO8601 日期的偏移量

C# get offset for ISO8601 date from Windows settings

我必须在我正在使用的 C# 项目中最大限度地使用 ISO8601 日期,尤其是在数据库中保存日期时。

在我创建 Outlook 约会的同一个项目中。

List<string> Recipients = new List<string>();
Recipients.Add("person@place.com");

string AppointmentSubject = "test subject";

string AppointmentBody = "test body";

string AppointmentLocation = "test location"; // Where do I get a list of meeting rooms?

string Offset = GetISO8601OffsetForThisMachine();

DateTime AppointmentStart = DateTime.Parse("2016-10-07T08:00:00" + Offset);

DateTime AppointmentEnd = DateTime.Parse("2016-10-07T10:00:00" + Offset);

Boolean IsAtDesk = true;

CreateOutlookAppointment(Recipients, AppointmentLocation, AppointmentSubject, AppointmentBody, AppointmentStart, AppointmentEnd, IsAtDesk);

在英国,由于夏令时,我们的偏移量是 +1 或 +0。

有没有办法根据 Windows 区域设置以编程方式获取偏移量?

你或许可以这样做:

DateTime AppointmentStart = DateTime.Parse("2016-10-07T08:00:00").ToLocalTime();

使用以下公式计算偏移量:

public static string GetISO8601OffsetForThisMachine()
{
    string MethodResult = null;
    try
    {
        TimeSpan OffsetTimeSpan = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);

        string Offset = (OffsetTimeSpan < TimeSpan.Zero ? "-" : "+") + OffsetTimeSpan.ToString(@"hh\:mm");

        MethodResult = Offset;

    }
    catch //(Exception ex)
    {
        //ex.HandleException();
    }
    return MethodResult;
}