asp.net 客户 显示当前日期时间 服务器 日期时间,如何在ASP.NET中获取客户端日期和时间?...

我喜欢使用浏览器/系统时间和时区或让他们选择其时区的想法。在过去的项目中,我使用了以下内容:

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

然后在服务器上:

///

/// Returns the client (if available in cookie) or server timezone.

///

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;

}

或者,您可以将其作为URL参数传递并在Page_Load中进行处理:

http://host/page.aspx?tz=-360

请记住要使用分钟,因为并非所有时区都是整个小时。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值