C# 如何在特定时区转换日期时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9869051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:00:24  来源:igfitidea点击:

How to convert DateTime in Specific timezone?

c#datetimeutc

提问by fiberOptics

I find it hard to understand how UTC works.

我发现很难理解 UTC 是如何工作的。

I have to do the following but I'm still confused if I'd get the right result.

我必须执行以下操作,但如果我得到正确的结果,我仍然感到困惑。

Objectives:

目标:

  1. Ensure all saved dates in Database are in UTC format
  2. Update DefaultTimezone is in Manila time
  3. Ensure all returned dates are in Manila Time
  1. 确保数据库中所有保存的日期都是 UTC 格式
  2. 更新 DefaultTimezone 是在马尼拉时间
  3. 确保所有返回的日期都在马尼拉时间

So the code is:

所以代码是:

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();
    }
}

Sample usage:

示例用法:

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

Now I get confused for 'TimeZone'. I don't know how to use it to get the objectives.
Hope you understand my question and have the idea to get the objectives done.

现在我对“时区”感到困惑。我不知道如何使用它来实现目标。
希望您理解我的问题并有完成目标的想法。

Edit

编辑

According to @raveturned answer, I get this following code:

根据@raveturned 的回答,我得到以下代码:

***Added in ConvertDate method

***在 ConvertDate 方法中添加

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

**New Property

**新物业

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

采纳答案by raveturned

The .NET framework already has classes and methods available to convert DateTimes between different time zones. Have a look at the ConvertTime methods of the TimeZoneInfoclass.

.NET 框架已经提供了可用于在不同时区之间转换 DateTime 的类和方法。查看TimeZoneInfo类的 ConvertTime 方法。

Edit:When you get the time to put into the database, assuming it was created with correct time zone information you can easily convert to UTC:

编辑:当您有时间放入数据库时​​,假设它是使用正确的时区信息创建的,您可以轻松转换为 UTC:

DateTime utcTime = inputDateTime.ToUniversalTime();

Get timeInfo as done in the question edit:

在问题编辑中获取 timeInfo:

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

When you send the database time to user, convert it to the correct timezone using timeInfo.

当您将数据库时间发送给用户时,请使用 将其转换为正确的时区timeInfo

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

Personally I'd try and keep this logic separate from the propery get/set methods.

我个人会尝试将此逻辑与属性 get/set 方法分开。

回答by Roger Tello

TimeZoneInfo infotime = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time (Mexico)");
DateTime thisDate = TimeZoneInfo.ConvertTimeFromUtc(datetimeFromBD, infotime);

回答by user3761555

To help others:

帮助他人:

    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;
    }

回答by Kevin

var date = System.TimeZoneInfo.ConvertTimeFromUtc(
    DateTime.UtcNow, 
    TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));