创建开始时采用太平洋时区且结束时采用东部时区的约会

有时,约会可能跨越一个时间段,在此期间,用户可能出差到与约会开始时不同的时区。 该示例创建在太平洋时区 (UTC-8) 开始、在东部时区 (UTC-5) 结束的约会。

示例

此代码示例使用表示 Microsoft Windows 中识别的所有时区的 TimeZones 对象。 它还使用 TimeZone 对象设置或获取 AppointmentItem 对象的 StartTimeZone 属性和 EndTimeZone 属性。

Outlook 采用本地时间显示所有日期,本地时间按用户的当前时区(由 Windows 控制面板中的用户设置控制)表示。 Outlook 还会设置或获取本地时间的属性,例如 “开始” 和“ 结束”。 但是,Outlook 将日期和时间值存储为协调世界时 (UTC) 而非本地时间。 如果使用 PropertyAccessor 对象检查 Appointment.Start 的内部值,你会发现内部日期和时间值等于转换为等效 UTC 日期和时间值的本地日期和时间值。

Outlook 在保存约会时,使用时区信息将约会映射到正确的 UTC 时间;在日历中显示项目时,映射到正确的本地时间。 更改 StartTimeZone 会影响 Appointment.Start 的值,该值始终以本地时区表示,由 TimeZones 返回的 对象的 CurrentTimeZone 属性表示。 类似地,更改 EndTimeZone 会影响 Appointment.End 的值,该值总是采用本地时区表示,本地时区由 Application.TimeZones 返回的对象的 CurrentTimeZone 属性表示。

可以通过使用 Windows 注册表中与区域设置无关的 TimeZone 项,从 TimeZones 对象中检索特定 TimeZone。 与区域设置无关的时区键列在以下项下: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\TimeZones

如果使用 Visual Studio 测试此代码示例,必须先添加对 Microsoft Outlook 15.0 对象库组件的引用,并在导入 Microsoft.Office.Interop.Outlook 命名空间时指定 Outlook 变量。 不得将 Importsusing 语句直接添加到此代码示例中的函数前面,这两个语句必须后跟公共类声明。 下面的代码行演示了如何在 Visual Basic 和 C# 中执行导入和分配。

Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
Private Sub TimeZoneExample()
    Dim appt As Outlook.AppointmentItem = _
        CType(Application.CreateItem( _
        Outlook.OlItemType.olAppointmentItem), Outlook.AppointmentItem)
    Dim tzs As Outlook.TimeZones = Application.TimeZones
    ' Obtain timezone using indexer and locale-independent key
    Dim tzEastern As Outlook.TimeZone = tzs("Eastern Standard Time")
    Dim tzPacific As Outlook.TimeZone = tzs("Pacific Standard Time")
    appt.Subject = "SEA - JFK Flight"
    appt.Start = DateTime.Parse("8/9/2006 8:00 AM")
    appt.StartTimeZone = tzPacific
    appt.End = DateTime.Parse("8/9/2006 5:30 PM")
    appt.EndTimeZone = tzEastern
    appt.Display(False)
End Sub
private void TimeZoneExample()
{
    Outlook.AppointmentItem appt = Application.CreateItem(
        Outlook.OlItemType.olAppointmentItem)
        as Outlook.AppointmentItem;
    Outlook.TimeZones tzs = Application.TimeZones;
    // Obtain timezone using indexer and locale-independent key
    Outlook.TimeZone tzEastern = tzs["Eastern Standard Time"];
    Outlook.TimeZone tzPacific = tzs["Pacific Standard Time"];
    appt.Subject = "SEA - JFK Flight";
    appt.Start = DateTime.Parse("8/9/2006 8:00 AM");
    appt.StartTimeZone = tzPacific;
    appt.End = DateTime.Parse("8/9/2006 5:30 PM");
    appt.EndTimeZone = tzEastern; 
    appt.Display(false);
}

另请参阅