如何转换日期时间在特定时区? [英] How to convert DateTime in Specific timezone?
问题描述
我觉得很难理解UTC是如何工作的。
我必须做到以下几点,但我仍然感到困惑,如果我能得到正确的结果。
目标:
- 确保所有数据库中保存的日期是UTC格式
- 更新DefaultTimezone是马尼拉时间
- 确保所有返回的日期是在马尼拉时间
因此,代码为:
公共ConvertDate(?日期时间DATETIME)
{
如果(日期时间!= NULL)
{
值=(DateTime的)日期时间;
TimeZone的= GetFromConfig.DefaultTimeZone();
}
}
公共ConvertDate(日期时间?日期时间,INT GMTTimeZone)
{
如果(日期时间!= NULL)
{
值=(DateTime的)日期时间;
TimeZone的= GMTTimeZone;
}
}
公众诠释的TimeZone
{
{返回m_TimeZone; }
集合{m_TimeZone =价值; }
}
的DateTime m_Value;
公共datetime值
{
{返回m_Value; }
组
{
m_Value =价值;
日期时间转换= m_Value.ToUniversalTime()ToLocalTime();
}
}
使用范例:
的DateTime SampleInputFromUser =新日期时间(2012年,1,22);
ConvertDate newConversion =新ConvertDate(SampleInputFromUser,21);
DateTime的答案= newConversion.Value;
现在我感到困惑的时区。我不知道如何使用它来获取目标。结果
希望你能理解我的问题,有想法来完成目标。
修改
据@raveturned答案,我得到这个下面的代码:
***上架在ConvertDate方法
的TimeZoneInfo timeInfo =的TimeZoneInfo .FindSystemTimeZoneById(GetFromConfig.ManilaTimeZoneKey());
ManilaTime = TimeZoneInfo.ConvertTime(dateTime.Value,TimeZoneInfo.Local,timeInfo).ToUniversalTime();
**新属性。
的DateTime _ManilaTime;
公众的DateTime ManilaTime
{
{返回_ManilaTime; }
集合{_ManilaTime =价值; }
}
在.NET框架已经有可用的转换不同的时区之间创建DateTime对象的类和方法。看一看在的TimeZoneInfo 类的ConvertTime方法。
修改:当你得到它与正确的时区信息创建放入数据库的时间,假设你可以很容易地转换为UTC:
的DateTime utcTime = inputDateTime.ToUniversalTime();
获取timeInfo如问题编辑完成的:
的TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById(GetFromConfig.ManilaTimeZoneKey());
当您发送数据库时的用户,它采用转换为正确的时区 timeInfo
的DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(dbDateTime,timeInfo);
个人而言,我会尽量保持这个逻辑从属性格式get / set方法分离出来。
I find it hard to understand how UTC works.
I have to do the following but I'm still confused if I'd get the right result.
Objectives:
- Ensure all saved dates in Database are in UTC format
- Update DefaultTimezone is in Manila time
- Ensure all returned dates are in Manila Time
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:
***Added in ConvertDate method
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; }
}
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 TimeZoneInfo class.
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:
DateTime utcTime = inputDateTime.ToUniversalTime();
Get timeInfo as done in the question edit:
TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById(GetFromConfig.ManilaTimeZoneKey());
When you send the database time to user, convert it to the correct timezone using timeInfo
.
DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(dbDateTime, timeInfo);
Personally I'd try and keep this logic separate from the propery get/set methods.
这篇关于如何转换日期时间在特定时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!