簡體   English   中英

如何在特定時區轉換日期時間?

[英]How to convert DateTime in Specific timezone?

我發現很難理解 UTC 是如何工作的。

我必須做以下操作,但是如果我得到正確的結果,我仍然感到困惑。

目標:

  1. 確保數據庫中的所有保存日期均為UTC格式
  2. 更新DefaultTimeZone在馬尼拉時間
  3. 確保所有返回的日期都在馬尼拉時間

所以代碼是:

public ConvertDate(DateTime? dateTime)
{
    if (dateTime != null)
    {
        Value = (DateTime)dateTime;
        TimeZone = GetFromConfig.DefaultTimeZone(); 
    }
}


public ConvertDate(DateTime? dateTime, int GMTTimeZone)
{
    if (dateTime != null)
    {
        Value = (DateTime)dateTime;
        TimeZone = GMTTimeZone;
    }
}


public int TimeZone
{
    get { return m_TimeZone; }
    set { m_TimeZone = value; }
}


DateTime m_Value;
public DateTime Value
{
    get { return m_Value; }
    set 
    { 
        m_Value = value;
        DateTime converted = m_Value.ToUniversalTime().ToLocalTime();
    }
}

示例用法:

DateTime SampleInputFromUser = new DateTime(2012, 1, 22);
ConvertDate newConversion = new ConvertDate(SampleInputFromUser, 21);
DateTime answer = newConversion.Value;

現在,我對“時區”感到困惑。 我不知道如何使用它來獲得目標。
希望您理解我的問題並有想法完成目標。

編輯

根據@raveturn的答案,我得到以下代碼:

***添加了轉換方法

TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById(GetFromConfig.ManilaTimeZoneKey());
ManilaTime = TimeZoneInfo.ConvertTime(dateTime.Value, TimeZoneInfo.Local, timeInfo).ToUniversalTime();

**新屬性

DateTime _ManilaTime;
public DateTime ManilaTime
{
    get { return _ManilaTime; }
    set { _ManilaTime = value; }
}

.NET框架已經具有可用於在不同時區之間轉換DateTime的類和方法。 看一下TimeZoneInfo類的ConvertTime方法。

編輯:當您有時間放入數據庫時​​,假設它是使用正確的時區信息創建的,則可以輕松轉換為UTC:

DateTime utcTime = inputDateTime.ToUniversalTime();

獲取問題編輯中的timeInfo:

TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById(GetFromConfig.ManilaTimeZoneKey());

將數據庫時間發送給用戶時,請使用timeInfo將其轉換為正確的時區。

DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(dbDateTime, timeInfo);

我個人將嘗試將此邏輯與適當的獲取/設置方法分開。

TimeZoneInfo infotime = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time (Mexico)");
DateTime thisDate = TimeZoneInfo.ConvertTimeFromUtc(datetimeFromBD, infotime);
var date = System.TimeZoneInfo.ConvertTimeFromUtc(
    DateTime.UtcNow, 
    TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));

幫助他人:

    static void ChangeTimezone()
    {
        // Timezone string here:
        foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
            Console.WriteLine(z.Id);

        // Use one of those timezone strings
        DateTime localDt = DateTime.Today;
        DateTime utcTime = localDt.ToUniversalTime();
        TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time");
        DateTime estDt = TimeZoneInfo.ConvertTimeFromUtc(utcTime, timeInfo);
        return;
    }

對於在跨平台獲取 TimeZoneInfo 時遇到問題的任何人(Windows 和 Linux 之間的不同時區 id),.NET 6 解決了這個問題:

從此版本開始,如果在系統上未找到請求的時區,TimeZoneInfo.FindSystemTimeZoneById 方法將自動將其輸入轉換為相反的格式。 這意味着您現在可以在任何安裝了時區數據的操作系統上使用 IANA 或 Windows 時區 ID*。 它使用相同的 CLDR 映射,但通過 .NET 的 ICU 全球化支持獲取它們,因此您不必使用單獨的庫。

一個簡單的例子:

// Both of these will now work on any supported OS where ICU and time zone data are available.
TimeZoneInfo tzi1 = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");
TimeZoneInfo tzi2 = TimeZoneInfo.FindSystemTimeZoneById("Australia/Sydney");

在此處查找更多信息

正如其他答案中所述:要從 UTC 獲取所需時區的DateTime ,請使用TimeZoneInfo.ConvertTimeFromUtc(DateTime, TimeZoneInfo) 方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM