使用查询字符串将 Javascript UTC 从客户端发送到经典 ASP 应用程序
Send Javascript UTC from client to Classic ASP application with Query String
我正在尝试将本地时间事件 - 上午 8 点 - 存储在数据库中 - 转换为客户在另一个国家/地区的当地时间。
- 例如,早上 8 点在墨西哥城到巴黎的活动。
我不懂 JS,但我可以用这个在客户端机器上识别本地时间:
<script>
function myFunction() {
var d = new Date();
var n = d.getTimezoneOffset();
}
</script>
现在我尝试将此值发送到 CLASSIC ASP - 返回服务器。
为此,我尝试创建一个 link 以在查询字符串中发送信息。
www.domain.com/login.asp?utc=XXXX
然后我可以从客户端获取 UTC 时间并使用 Classic ASP 进行转换(我更了解 Classic ASP)
另一种简单的方法是在 JS 中创建一个 SESSION - 但我不知道它是如何与 CLassic ASP 会话一起工作的。
有什么想法吗?
谢谢!
丹尼尔
我建议使用浏览器 cookie 和经典 ASP 的 Response.Cookies 集合来执行此操作。要做到这一点 Javascript 非常简单:
// get the current date from the user's local machine
var now = new Date();
/*
The getTimezoneOffset() function returns the timezone offset in minutes.
If you want to convert dates from UTC to the user's local time
based on the timezone the user has set on their machine you need to
multiple the result by -1.
This method does have the caveat that it assumes the user has set
their local machine's timezone information correctly.
*/
var offset = -now.getTimezoneOffset();
// write a cookie to the user's browser with the offset
document.cookie = "offset_to_utc="+offset
您可以检查cookie是否已经存在,而不是每次都检查用户的时区;但是,如果用户在同一会话期间更改了他们的时区信息,则不会反映更改。
在下一个页面加载和同一浏览器会话中的任何页面加载时,您可以使用 Response.Cookies:
检索刚刚在服务器端设置的 cookie
Dim OffsetToUTC
OffsetToUTC = Response.Cookies("offset_to_utc").Value
'Since cookies can be modified by users, it is a good idea to check the type
'of the data retrieved and convert it to an integer only if it is numeric.
If IsNumeric(OffsetToUTC) Then
OffsetToUTC = CInt(OffsetToUTC)
Else
OffsetToUTC = 0
End If
要将数据库中以 UTC 格式存储的日期转换为用户本地时间,如下所示:
Dim DBDate
DBDate = '<Replace with your code to get the date from the database>'
Response.Write(DateAdd("n", OffsetToUTC, DBDate))
我正在尝试将本地时间事件 - 上午 8 点 - 存储在数据库中 - 转换为客户在另一个国家/地区的当地时间。
- 例如,早上 8 点在墨西哥城到巴黎的活动。
我不懂 JS,但我可以用这个在客户端机器上识别本地时间:
<script>
function myFunction() {
var d = new Date();
var n = d.getTimezoneOffset();
}
</script>
现在我尝试将此值发送到 CLASSIC ASP - 返回服务器。
为此,我尝试创建一个 link 以在查询字符串中发送信息。
www.domain.com/login.asp?utc=XXXX
然后我可以从客户端获取 UTC 时间并使用 Classic ASP 进行转换(我更了解 Classic ASP)
另一种简单的方法是在 JS 中创建一个 SESSION - 但我不知道它是如何与 CLassic ASP 会话一起工作的。
有什么想法吗? 谢谢!
丹尼尔
我建议使用浏览器 cookie 和经典 ASP 的 Response.Cookies 集合来执行此操作。要做到这一点 Javascript 非常简单:
// get the current date from the user's local machine
var now = new Date();
/*
The getTimezoneOffset() function returns the timezone offset in minutes.
If you want to convert dates from UTC to the user's local time
based on the timezone the user has set on their machine you need to
multiple the result by -1.
This method does have the caveat that it assumes the user has set
their local machine's timezone information correctly.
*/
var offset = -now.getTimezoneOffset();
// write a cookie to the user's browser with the offset
document.cookie = "offset_to_utc="+offset
您可以检查cookie是否已经存在,而不是每次都检查用户的时区;但是,如果用户在同一会话期间更改了他们的时区信息,则不会反映更改。
在下一个页面加载和同一浏览器会话中的任何页面加载时,您可以使用 Response.Cookies:
检索刚刚在服务器端设置的 cookieDim OffsetToUTC
OffsetToUTC = Response.Cookies("offset_to_utc").Value
'Since cookies can be modified by users, it is a good idea to check the type
'of the data retrieved and convert it to an integer only if it is numeric.
If IsNumeric(OffsetToUTC) Then
OffsetToUTC = CInt(OffsetToUTC)
Else
OffsetToUTC = 0
End If
要将数据库中以 UTC 格式存储的日期转换为用户本地时间,如下所示:
Dim DBDate
DBDate = '<Replace with your code to get the date from the database>'
Response.Write(DateAdd("n", OffsetToUTC, DBDate))