指定日期时间的时区而不更改值 [英] Specify timezone of datetime without changing value

查看:88
本文介绍了指定日期时间的时区而不更改值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在不实际更改值的情况下更改DateTime对象的时区.这是背景...

I'm wondering how to go about changing the timezone of a DateTime object without actually changing the value. Here is the background...

我在AppHarbor上托管了一个ASP.NET MVC站点,该服务器的时间设置为UTC.当我从网站提交包含日期时间值的表单时,例如2013年9月17日4:00,它将以该值上载到服务器.但是,当我这样做时:

I have an ASP.NET MVC site hosted on AppHarbor and the server has its time set to UTC. When I submit a form from my site containing a datetime value, say 9/17/2013 4:00am, it comes up to the server with that value. However then when I do:

public ActionResult Save(Entity entity)
{
    entity.Date = entity.Date.ToUniversalTime();
    EntityService.Save(entity);
}

...由于服务器已经处于UTC时间,因此错误地将其保持为同一时间(凌晨4点).因此,在转换为UTC并保存到数据库后,数据库值是2013-09-17 04:00:00.000,实际上它应该是2013-09-17 08:00:00.000,因为浏览器处于EST上.我希望提交表单并将这些值传递给控制器​​操作后,它将把DateTime的时区设置为浏览器的(EST)而不是服务器主机的(UTC),但是那没有发生.

...it incorrectly keeps it as the same time (4am) because the server is already on UTC time. So after converting to UTC and saving to the database, the database value is 2013-09-17 04:00:00.000, when really it should be 2013-09-17 08:00:00.000, as the browser is on EST. I was hoping after form submission and passing the values to the controller action, it would set the DateTime's timezone to the browser's (EST) rather than the server host's (UTC), however that didn't happen.

无论如何,现在我陷入了一个DateTime对象,该对象包含正确的日期/时间值但错误的时区,因此无法正确地将其转换为UTC.我存储了每个用户的时区,因此,如果有一种方法可以设置DateTime对象的时区而不实际更改值(我不能执行TimeZoneInfo.ConvertTime,因为它将更改值),那么我可以正确的日期/时间值和正确的时区,那么我可以放心地做date.ToUniversalTime.话虽如此,我只看到了如何更改DateTime(而不是TimeZone)的KIND,而不更改其实际值.

In any case, now I'm stuck with a DateTime object that contains the correct date/time value but the wrong time zone, so I can't correctly convert it to UTC. I store the time zone of each user, so if there was a way to set the time zone for the DateTime object without actually changing the value (I can't do TimeZoneInfo.ConvertTime because it'll change the value) then I could have the correct date/time value AND the correct time zone, then I could safely do date.ToUniversalTime. That being said, I've only seen how to change the KIND of a DateTime, not the TimeZone, without changing its actual value.

有什么想法吗?

推荐答案

实际上,它确实在执行您要求的操作.从服务器的角度来看,您向它传递了一个未指定"的DateTime.换句话说,只是年份,月份等,而没有任何上下文.如果查看.Kind属性,您会发现它确实是DateTimeKind.Unspecified.

Actually, it is doing exactly what you asked it to do. From the point of view of the server, you passed it an "unspecified" DateTime. In other words, just year, month day, etc., withoutout any context. If you look at the .Kind property, you will see that it is indeed DateTimeKind.Unspecified.

在未指定类型的DateTime上调用.ToUniversalTime()时,.NET将假定运行代码的计算机的本地时区的上下文.您可以在此处的文档中详细了解.由于您的服务器设置为UTC,因此无论输入类型如何,这三种类型都会产生相同的结果.基本上,这是无人操作.

When you call .ToUniversalTime() on an unspecified kind of DateTime, .NET will assume the context of the local time zone of the computer that the code is running on. You can read more about this in the documentation here. Since your server is set to UTC then regardless of the input kind, all three kinds will yield the same result. Basically, it's a no-op.

现在,您说您想要时间来反映用户的时区.不幸的是,这是服务器所没有的信息.没有神奇的HTTP标头可以包含时区详细信息.

Now, you said that you wanted the time to reflect the user's time zone. Unfortunately, that is information that the server doesn't have. There's no magic HTTP header that carries time zone details.

尽管有多种方法可以达到这种效果,但是您有一些选择.

There are ways to achieve this effect though, and you have some options.

JavaScript方法

这可能是最简单的选项,但是它确实需要JavaScript.

This is probably the easiest option, but it does require JavaScript.

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆