C# 如何在 ASP.NET 中获取客户端日期和时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/274826/
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
How to get client date and time in ASP.NET?
提问by Arief
When I use DateTime.Now
I get the date and time from the server point of view. Is there any way to get the clientdate and time in ASP.NET?
当我使用时,DateTime.Now
我从服务器的角度获取日期和时间。有没有办法在 ASP.NET 中获取客户端日期和时间?
采纳答案by Simon Johnson
What I'd do is create a hidden input field and then wire a Javascript routine to the onsubmit event for the form. This routine would populate the hidden field with the time on the client machine.
我要做的是创建一个隐藏的输入字段,然后将一个 Javascript 例程连接到表单的 onsubmit 事件。此例程将使用客户端计算机上的时间填充隐藏字段。
The hidden field can used with ASP.NET by using the HTML control "HtmlInputHidden" class. You just give you input control a runat="server" attribute like any other server side control.
通过使用 HTML 控件“HtmlInputHidden”类,隐藏字段可以与 ASP.NET 一起使用。您只需为输入控件提供一个 runat="server" 属性,就像任何其他服务器端控件一样。
The server can then read out this time when the form posts back. You could even wrap this up in a server control if you need to do this in a number of places.
当表单回发时,服务器可以读出这个时间。如果您需要在多个地方执行此操作,您甚至可以将其封装在服务器控件中。
Alternatively, you could do this with AJAX but the implementation will depend on which library you use.
或者,您可以使用 AJAX 执行此操作,但实现将取决于您使用的库。
回答by Stephen Wrighton
if you're maintaining a user profile, you can ask them to tell you their timezone, and then do the calculations necessary.
如果您正在维护用户配置文件,您可以要求他们告诉您他们的时区,然后进行必要的计算。
回答by Keltex
The alternative is to geolocate the user based on his/her IP address. Or geolocate if the browser has this capability (coming soon for Firefox). Once you have the user's location you can lookup the timezone.
另一种方法是根据他/她的 IP 地址对用户进行地理定位。或者,如果浏览器具有此功能,则进行地理定位(Firefox 即将推出)。获得用户的位置后,您可以查找时区。
The javascript solution is probably a good and easy one.
javascript 解决方案可能是一个很好且简单的解决方案。
回答by JacquesB
Client and server may not be totally synchronized, so the question is if you want the time on the client computer, or you want the time on the server, but adjusted for time-zone differences. Javascript can get you the time on the client (including timezone). You can also combine the time on the server with the timezone of the client.
客户端和服务器可能不完全同步,所以问题是您是否想要客户端计算机上的时间,或者您想要服务器上的时间,但根据时区差异进行调整。Javascript 可以让您获得客户端上的时间(包括时区)。您还可以将服务器上的时间与客户端的时区结合起来。
You can never get the time with higher precision than the latcency of a request, though.
但是,您永远无法获得比请求延迟更精确的时间。
回答by Ryan
I like the idea of either using the browser/system time and time zone or letting them select their time zone. In a past project I used something like this:
我喜欢使用浏览器/系统时间和时区或让他们选择时区的想法。在过去的项目中,我使用了这样的东西:
<script language="javascript">
function checkClientTimeZone()
{
// Set the client time zone
var dt = new Date();
SetCookieCrumb("ClientDateTime", dt.toString());
var tz = -dt.getTimezoneOffset();
SetCookieCrumb("ClientTimeZone", tz.toString());
// Expire in one year
dt.setYear(dt.getYear() + 1);
SetCookieCrumb("expires", dt.toUTCString());
}
// Attach to the document onload event
checkClientTimeZone();
</script>
And then on the server:
然后在服务器上:
/// <summary>
/// Returns the client (if available in cookie) or server timezone.
/// </summary>
public static int GetTimeZoneOffset(HttpRequest Request)
{
// Default to the server time zone
TimeZone tz = TimeZone.CurrentTimeZone;
TimeSpan ts = tz.GetUtcOffset(DateTime.Now);
int result = (int) ts.TotalMinutes;
// Then check for client time zone (minutes) in a cookie
HttpCookie cookie = Request.Cookies["ClientTimeZone"];
if (cookie != null)
{
int clientTimeZone;
if (Int32.TryParse(cookie.Value, out clientTimeZone))
result = clientTimeZone;
}
return result;
}
Or you can pass it in as a URL parameter and handle it in the Page_Load:
或者您可以将其作为 URL 参数传入并在 Page_Load 中处理它:
http://host/page.aspx?tz=-360
Just remember to use minutes, since not all time zones are whole hours.
请记住使用分钟,因为并非所有时区都是整小时。
回答by Yoshida Hiro
I used this method in ASP.Net with VB
我在 ASP.Net 和 VB 中使用了这种方法
Dim strLanguage As String = Request.UserLanguages(0)
Dim currentCulture As CultureInfo = CultureInfo.CreateSpecificCulture(strLanguage)
Dim dateformat As String = currentCulture.DateTimeFormat.ShortDatePattern
This will yield the data time format of the computer looking at the data.
这将产生查看数据的计算机的数据时间格式。