如何做到在ASP.NET MVC时区? [英] How to do timezones in ASP.NET MVC?

查看:586
本文介绍了如何做到在ASP.NET MVC时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站,我需要知道什么时区的人都位于中,为了在正确的时间,以显示信息给他们。我也不太清楚关于如何做到这一点的教程方面要搜索的内容。

On my site, I need to know what timezones people are located in, in order to display messages to them at the right times. I am not too sure what to be searching for in terms of a tutorial on how to do this.

什么我打算做的是:当用户来到我的网站,他们将通过从下拉列表中选择设置自己的时区。我将保存他们的设置在我的数据库,并使用它来计算时间。

What I am planning on doing is: when a user comes to my site, they will set their timezone by selecting it from a dropdown list. I will store their settings in my database and use it to calculate the times.

我不知道我需要什么。应该怎样我的数据库时存储?我读的地方,它应该被存储为UTC。如何设置我的数据库来做到这一点?另外,我将是一个共享的主机的网站,以便它的方式,我可以做到这一点(可能通过webconfig?)进行设置。

I am not sure what I need. What should be my database time be storing? I read somewhere that it should be stored as UTC. How do I set up my database to do this? Also, I will be on a shared hosting site so it has to be set in a way that I can do it (maybe through webconfig?).

接下来,我需要所有的时区为要么是HTML辅助或常规的HTML列表。我真的不想做的。

Next, I would need a list of all the timezones as either an HTML helper or regular HTML. I really don't want to make one.

然后,就像我说的,我必须展示我做一个教程。我认为这是增加,并从该时间减去的东西,但我可能是错的。

Then, like I said, I need a tutorial showing me what to do. I think it is adding and subtracting stuff from that time, but I could be wrong.

推荐答案

为了这个目的,你绝对应该储存您的时间戳在数据库UTC。

For this purpose, you should definitely be storing your timestamps in UTC in the database.

当你需要在网站上显示来自数据库的日期,你可以这样做:

When you need to display a date from the database on your site, you can do this:

DateTime stamp = /* get datetime from the database here, make sure you
                    use the constructor that allows you to specify the 
                    DateTimeKind as UTC. */

//E.g.
//DateTime stamp = new DateTime(2009, 12, 12, 12, 12, 12, DateTimeKind.Utc);

timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(" /* users time zone here */"); 
var convertedTime = TimeZoneInfo.ConvertTime(stamp, timeZoneInfo);

//Print out the date and time
//Console.WriteLine(convertedTime.ToString("yyyy-MM-dd HH-mm-ss"));

时区的名单在.NET中已经可用,所以你可以看到的这个职位上如何一一列举。对于ASP.Net MVC,而不是打印出来的时候,你想转换的日期时间分配给你的模型类的属性,以便您的视图可以使用它来显示。

The list of timezones is already available in .Net, so you can see this post on how to enumerate them. For ASP.Net MVC, instead of printing the time out, you would want to assign the converted datetime to a property of your model class so your View could use it for display.

这篇关于如何做到在ASP.NET MVC时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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