我必须在我正在使用的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区域设置以编程方式获取偏移量?
答案 0 :(得分:0)
您可以执行以下操作:
DateTime AppointmentStart = DateTime.Parse("2016-10-07T08:00:00").ToLocalTime();
答案 1 :(得分:0)
使用以下内容计算偏移量:
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;
}