现在是墨尔本时间下午5:30。洛杉矶现在是凌晨12:30。
此时创建了一条数据库记录。它有一个"creation_datetime"时间戳。
当墨尔本的客户查看时间戳时,我希望显示"2016-07-04 17:30 (UTC+10:00)"。当客户在LA查看时间时,我希望显示"2016-07-04 00:30 (UTC-8:00)"。
让我们假设函数"getRecordTimeStamp(123)"返回该时间戳作为DateTime,其中"123"是记录ID。如何在c#中生成"2016-07-04 17:30 (UTC+10:00)"one_answers"2016-07-04 00:30 (UTC-8:00)"?
我算出来了:
DateTime now = DateTime.Now;
DateTime utcTimeNow = TimeZoneInfo.ConvertTimeToUtc(now);
TimeZoneInfo melbourneTimeZone = TimeZoneInfo.FindSystemTimeZoneById("E. Australia Standard Time");
DateTime melbourneTime = TimeZoneInfo.ConvertTimeFromUtc(utcTimeNow, melbourneTimeZone);
Console.WriteLine("The date and time are {0} {1}.", melbourneTime, melbourneTimeZone.IsDaylightSavingTime(melbourneTime) ? melbourneTimeZone.DaylightName : melbourneTimeZone.StandardName);
TimeZoneInfo laTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
DateTime laTime = TimeZoneInfo.ConvertTimeFromUtc(utcTimeNow, laTimeZone);
Console.WriteLine("The date and time are {0} {1}.", laTime, laTimeZone.IsDaylightSavingTime(laTime) ? laTimeZone.DaylightName : laTimeZone.StandardName);
Console.ReadKey();