我有一堆日期时间,我跟踪我的应用程序.它们都是UTC时间.对于我的应用程序的一部分,我想发送一封包含其中一个时间的电子邮件,但我已编辑到该特定时区.
我将只涉及两个主要领域,东海岸和德克萨斯(达拉斯和休斯顿)
当我发送此电子邮件以获取东部时区时,我也可以创建一个新的日期时间(DateTime timestamp = DateTime.Now;)
我的问题是:
如果用户在德克萨斯州,我怎样才能将我的时间从东部转换到那个时间(少1小时)?
我试过这样的事情:
//Convert timestamp to local time
TimeSpan ts = TimeZone.CurrentTimeZone.GetUtcOffset(timestamp);
timestamp.Add(ts);
timestampString = timestamp.ToString();
Run Code Online (Sandbox Code Playgroud)
但那没用.我也知道这条线无效:
timestamp.Hour = timestamp.Hour - 1;
Run Code Online (Sandbox Code Playgroud)
Mar*_*tin 15
使用TimeZoneInfo类将本地时间转换为备用时区中的时间:
TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime targetTime = TimeZoneInfo.ConvertTime(timeToConvert, est);
Run Code Online (Sandbox Code Playgroud)
小智 5
var now = DateTime.Now; // Current date/time
var utcNow = now.ToUniversalTime(); // Converted utc time
var otherTimezone = TimeZoneInfo.FindSystemTimeZoneById("ANY OTHER VALID TIMEZONE"); // Get other timezone
var newTime = TimeZoneInfo.ConvertTimeFromUtc(utcNow, otherTimezone); // New Timezone
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12541 次 |
| 最近记录: |